1

I have the following code:

html template:

<button class=""  ng-click="item_popup.toggle()"/>

inside one directive:

app.directive('itemPopup', ['$sce', '$timeout', function($sce, $timeout){
    return {
        restrict: 'E',
        replace: true,
        templateUrl: $sce.trustAsResourceUrl('/'),
        link: function(scope, el, attrs) {

    toggle = function(){
        item.toggle = scope.items.checked == 0 ? 1 : 0
    }
}]);

and app:

app.directive('item', ['$filter','$timeout', function($filter,$timeout){
        scope.$watch('item.toggle', function(newValue,oldValue){
            if(newValue!=undefined && oldValue!=undefined && oldValue != newValue){
                // Do something 
            }
      });
}]);

the issue here is that each time toggle function is called, scope.$watch is triggered twice.

i tried to check some other answers, like:

Angular expressions in $watch trigger twice

$rootScope.$watch triggered twice

but non of them gives the answer.

Eyal Ch
  • 9,552
  • 5
  • 44
  • 54
  • How is the directive being used in the HTML template? – Jon May 24 '17 at 16:54
  • 1
    When asking a question about a problem caused by your code, you will get much better answers if you provide code people can use to reproduce the problem. See [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – georgeawg May 24 '17 at 16:54
  • added html template code – Eyal Ch May 24 '17 at 17:00
  • Both the `item-popup` directive and `item` directive are incomplete. And there is no example of how either directive is used. It is normal for watches to be called multiple times per digest cycle if the model propagates changes. – georgeawg May 25 '17 at 01:09
  • I'm voting to close this question as off-topic because there is not enough information to reproduce the problem. – georgeawg May 25 '17 at 01:11

0 Answers0