javascript - ngChange for multiple ngModels -
is there way call function if 1 of multiple ngmodel
values change, without adding $scope.$watch
?
change this:
<div> <input ng-model="value1" ng-change="update()"> <input ng-model="value2" ng-change="update()"> </div>
to this:
<div ng-changes="update()"> <input ng-model="value1" > <input ng-model="value2"> </div>
inspired simple implementation of ngchange
, can traverse child tree, collect ngmodelcontrollers
, register callback in $viewchangelisteners
.
function ngchanges() { return { restrict: 'a', link: function(scope, element, attr) { array.prototype.foreach.call(element[0].queryselectorall("*"), function(e) { var ngmodel = angular.element(e).controller('ngmodel'); if (ngmodel) { ngmodel.$viewchangelisteners.push(function() { scope.$eval(attr.onchanges); }) } }) } } };
Comments
Post a Comment