Question

To validate a form in Angularjs, the "name" attribute needs to be set on the form. When the "name" attribute is set, setting default values in the controller won't work anymore.

Here's an example to demonstrate: http://plnkr.co/edit/Q59yBZFIGWPMveYuez52?p=preview

On the first form, I have set no "name" attribute, which will set the default value correct, but will not have a validation object when I submit the value. On the second form, I have set the "name" attribute, which will not set the default value correct, but will have a validation object when I submit the value.

Is this expected behavior or a bug?

Was it helpful?

Solution

The form controller is put in the $scope with the given name. So in your case, the name of the model collides with the name of the form and strange things happen; this is a timebomb, a bug that will haunt you sometime in the future.

I like giving forms a distinct name; if you try <form name="user2form"> in your fiddle, it will work. (I always suffix the form name with form for this reason.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top