Form's state is pristine, although all checkboxes are selected

2,076 views
Skip to first unread message

Nadia Markadoni

unread,
Feb 12, 2013, 11:11:35 AM2/12/13
to ang...@googlegroups.com
Here is a jsfiddle of what i am trying to do:

I have a list of checkboxes and  "select all"/"unselect all" links which make all check boxes selected or not, but form state remains pristine.
In case of clicking on a single item state of form changes

Nadia Markadoni

unread,
Feb 12, 2013, 11:22:25 AM2/12/13
to ang...@googlegroups.com
here is a simpler example of the problem i am facing: http://jsfiddle.net/f5scR/

Why form's state does not change when chechbox's value is changing through a ng-click function?

Peter Bacon Darwin

unread,
Feb 12, 2013, 1:11:43 PM2/12/13
to ang...@googlegroups.com
Hi Nadia,
The answer is that the pristine state is only changed when the input itself updates the model, not when you update the model programmatically.  This makes sense because otherwise, when you set the value of the model initially it would make the input dirty straight away.
If you want to make the input dirty programmatically you need to get hold of the input's ngModelController.  The easiest way to do this, without writing a directive, is to note that by giving the form and the input's names they appear on the scope.  So in the case of the following fiddle: scope.myForm.value1.  Then you can do things like:

scope.myForm.value1.$setViewValue(...); // which will update the value and trigger the validation and dirty the input

or

scope.myForm.value1.$setDirty();

Here is the fiddle:



--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Anjo Krank

unread,
Feb 12, 2013, 2:43:46 PM2/12/13
to ang...@googlegroups.com
I'm probably confused but how can you unit test such a controller? The form won't be there without the mark up, will it?

Cheers, Anjo

Peter Bacon Darwin

unread,
Feb 13, 2013, 7:04:38 AM2/13/13
to ang...@googlegroups.com
You can mock up the formController and the ngModelController and with a spy on $setViewValue and check that it gets called.
Alternatively you hide all this in a directive on the ng-click element where you pass in the name of the input in question.
Reply all
Reply to author
Forward
0 new messages