Angular - ng-change not firing when ng-model is changed

tomazahlin picture tomazahlin · Jul 17, 2014 · Viewed 45.9k times · Source

The input is the following:

<input type="text" ng-model="repair.test"  ng-change="action()" />

The action() is executed when I manually type and change the input. However if I change the repair.test value by some other function programmatically, it doesnt fire the ng-change's action. I have read the angular tutorial and it's probably the expected behavior.

https://docs.angularjs.org/api/ng/directive/ngChange

"The expression is not evaluated when the value change is coming from the model." <- I need this too. How can I fire an action in controller, when the model changes in any way? (typing in input or by any other function)

Thanks for the help.

Edit:

The model value is actually the form.$valid, which has it's own Form controller around it (I think), that is why I used the action function to try to pass the value to the parent controller. So $scope.$watch at the moment doesn't work, only when it is initialised.

Answer

ThomasP1988 picture ThomasP1988 · Jul 17, 2014

ngChange is just for the input, if you want to listen the model do like this

$scope.$watch('repair.test', function(newvalue,oldvalue) {

            });