I'm implementing a simple spinner control in AngularJS and I want to react both on user input and changes from +/- buttons. Here is my HTML:
<input type='button' ng-click="MyProperty = MyProperty - 1" value="-">
<input type='text' ng-model="MyProperty" ng-change="log('changed from ngChange')">
<input type='button' ng-click="MyProperty = MyProperty + 1" value="+">
But this will track only 'user-changes' far as ngChange
supports only user-interaction updates as per documentaiton
So now I'm looking at $scope.$watch
as Frederik recommends:
$scope.$watch('MyProperty', function() {
$scope.log('changed from $watch');
});
See plunker demo
But this doesn't seem right enogh.
- First it's not declarative and you have to search the code for
MyTestProperty
to find this binding. - If you want would like to place
$scope.log
in a separate Model you have to either inject$scope
or to do the binding in controller. And as far as I understand both ways are not considered to be the best practicies.
Some people think that $watch
is a bad thing in general for a number of other reasons. But the solution advised there (which would be calling log
in ngClick directly) doesn't make too much diference to me. Basicly you have to manually track all the changes and if new actor comes you have to copy your logic there.
So the questions would be: is there a way that allows you to automaticly keep track of model updates without $watch? And how bad is the idea to implement your own derective for this if there is now such way?