Reputation: 32818
I have code like this:
<div ng-click="wos.wordWordFormRowClicked(wf, $index)"
ng-form="wordFormNgForm_{{$index}}"
ng-repeat="wf in wos.word.wordForms">
<div ng-show="wordFormNgForm_{{$index}}.$pristine == true">Pristine</div>
...
This sets up one or more forms.
Is there a way that I can check in a loop each of the forms that are created to see if it is still pristine? I know I can add see on the form the setting of $pristine but what I want is something that will cycle through all the forms from my service. For example here it might be 2,3, 4 or more forms with names like this:
wordFormNgForm_1
wordFormNgForm_2
wordFormNgForm_3
wordFormNgForm_4
Or however many of these that have been set up. But I am not sure of a way to check through each of them. I'm not sure if this helps but these forms are located inside another form:
<div ng-form="wos.wordNgForm">
<div ui-view></div>
</div>
Upvotes: 0
Views: 44
Reputation: 1579
I think what you're after is the $setPristine() method. on a form controller this method exists to set a form (entirely, including all children/child forms) to pristine. there are others which may be of use too you can read up about here. This will not reset the contents of the form, You will have to do that as an additional task, if you want to. The idea of $setPristine is to say "hey, this form is untouched by the user" even if it is... It could be the forms new starting point, for example after a user has set some defaults or something like that. It does exactly what it says on the tin, simply sets everything to pristine again.
For example from myController whose scope contains myForm you can do
$scope.myForm.$setPristine();
Hey presto, Pristine-them-up, all the way down!
This will only help you if you dont need to know exactly which subforms are dirty or not, and just want to reset them all to Pristine.
In my HTML I have the following:
<form id="myForm" name="myForm" ng-controller="MyController as myCtrl">
<ng-form name="child_form_{{::id}}" ng-repeat="id in ids">
<!--Stuff here -->
</ng-form>
</form>
where $scope.ids is a list of whatever (probably an array of numbers in this instance)
in this instance, if you want to know what sub forms are dirty, You have your list on scope, so from within the controller you have to loop through your $scope.ids and get your forms by re-creating the name, for example child_form_1. (But I see little benefit in knowing "which" child forms are dirty if your goal is to reset them all to pristine)
Upvotes: 1