I'm trying to achieve auto form validation without any submit. For this reason, I added the $watch in my form.
$scope.$watch('myForm.$valid', function(newVal, oldVal) {
console.log('watching....');
/*
If valid
perform action
*/
});
My form structure is:
<ul ng-controller="Ctrl">
<li ng-repeat='user in userList track by $index'>
<p>
User {{$index+1}}: {{user.userType}} <br/>
Designation: {{user.designation}}
</p>
<form name="myForm" class="my-form">
<label>Age?:</label>
<input name="userAge" type="number" ng-model="user.age" required/>
</form>
</li>
<button style="margin-top:20px;" type="button" ng-click="addUser()">Add more user</button>
</ul>
I can create multiple forms clicking the Add more user
button. My form is under userList
:
$scope.userList = [
{
userType: 'Doctor',
designation: 'Sr. cardiologist'
}
]
Problem
I'm unable to print the console.log()
inside my watcher if the form is valid/invalid. It only prints during page load. But if I made any changes to the form it doesn't show any console print.
But if I keep my form outside the ng-repeat scope it works. it prints the console item based on the validity.
**So why it's not working inside ng-repeat?**How can I check my form that I created is valid. What can I do to make it work? any suggestions or example will be appreciated