angularjs - ngChange in custom directive fire twice -
attached custom directive let using ng-change attribute.
the problem is, when using twice in same view, notice bar method fire twice, found problem $viewchangelisteners store same method of controller twice. note: notice maybe i've same directive twice 2 different ng-change methods
have 1 idea on problem?
html:
<my-directive ng-model="foo" items="items" ng-change="bar1(foo)"></my-directive> <my-directive ng-model="foo2" items="items" ng-change="bar2(foo2)"></my-directive> directive:
template: '<div ng-repeat="item in items" ng-click="updatemodel(item)">{{item}}</div>', require: "ngmodel", scope : { items : "=" }, link: function(scope, element, attrs, ctrl){ scope.updatemodel = function(item) { ctrl.$setviewvalue(item); } ctrl.$viewchangelisteners.push(function() { scope.$eval(attrs.ngchange); }); } try one, click on (1), click on down (1), click on (1). events fires following: bar1, bar2, bar1, bar2
when custom input directive "supports" ngmodel - uses ngmodel pipeline set view value - automatically gets benefit of supporting other directives use ngmodel, ng-required, ng-maxlength, and ng-change.
and so, following is not needed:
ctrl.$viewchangelisteners.push(function() { scope.$eval(attrs.ngchange); }); in particular example not fire bar1()/bar2() handlers twice, because scope.$eval(attrs.ngchange) evaluated inside isolate scope doesn't have bar1() , bar2(). so, perhaps happened in testing before used isolate scope.
Comments
Post a Comment