Problem: I simply want the value to be cleared from the input.
Solution: There is a workaround below.
I have this input and it is bound to a model which is held within the controller and a function in the controller sets it to empty (”) but the input still has the value set. I’ve seen to try use $scope.form.$setPristine(); to clear the form.
Now I see this error:
“TypeError: Cannot call method ‘$setPristine’ of undefined”
From the docs: “Each form directive creates an instance of FormController.” So does this mean it only works with a form directive? It’s not in the api docs but the setPristine() function uses the form name as the controller reference.
If I log the values of the form I get this:
1 2 3 4 5 |
console.log($scope); //object with no form present console.log($scope.newTagForm); //undefined console.log(newTagForm); //DOM object of form |
And if I use any of the following i get that error above:
1 2 3 4 |
$scope.newTagForm.$setPristine(); newTagForm.$setPristine(); |
In the end the reset worked fine so I’m using that for now. Still no idea why setPristine() is not working. If you have any ideas please leave a comment. Thanks.
1 2 3 4 |
newTag = ''; //model data clear newTagForm.reset(); //form input data clear |
refs:
i have exactly same problem…and however mange to use reset()…but still my required form validation shows
I found a solution:First,define a button in the form: button(ng-click=commit(form)), then write something in the controller:$scope.commit = function(){ form.$setPristine();} Hope it helps.
Thanks Maggie, i’ll try it and share when I get a chance.