Reputation: 355
I would like a function to set all fields untouched, not dirty and valid on a given form.I need that so i can display errors properly.Here is what i have tried.It does not work for some reason.
angular.forEach($scope.LoginForm, function (input) {
if (input.hasOwnProperty('$viewValue'))
input.$setPristine();
});
As you can see, a ngmodel on a form alwyas has a property called $viewValue.
I am getting the following error.
TypeError: Cannot read property 'hasOwnProperty' of undefined
Upvotes: 1
Views: 4131
Reputation: 1
Refactoring example:
angular.forEach(form.$$controls, function (input) {
if (input.$viewValue === undefined) {
input.$setPristine();
}
});
Upvotes: 0
Reputation: 1967
Certainly $scope.LoginForm
has an enumerable property that is undefined (or whose value is undefined
), so you can test for that:
angular.forEach($scope.LoginForm, function (input) {
if (input && input.hasOwnProperty('$viewValue')) {
input.$setPristine();
}
});
Maybe you can even test that input.hasOwnProperty
is a proper function using typeof input.hasOwnProperty === 'function'
but that may be pushing it a little. Instead, look at what's in the LoginForm object and go from there. Or even better, maybe look in the Angular docs if Angular has a proper way of iterating over form models?
Upvotes: 1