Thanks Sander for your prompt reply. 

I don't think I made myself clear.  In ngIf, there is a watcher defined:-

$scope.$watch($attr.ngIf, function ngIfWatchAction(value) {

          if (value) {
            if (!childScope) {
              $transclude(function(clone, newScope) {
                childScope = newScope;
                clone[clone.length++] = document.createComment(' end ngIf: 
' + $attr.ngIf + ' ');
                // Note: We only need the first/last node of the cloned 
nodes.
                // However, we need to keep the reference to the jqlite 
wrapper as it might be changed later
                // by a directive with templateUrl when its template 
arrives.
                block = {
                  clone: clone
                };
                $animate.enter(clone, $element.parent(), $element);
              });
            }
          } else {
            if (previousElements) {
              previousElements.remove();
              previousElements = null;
            }
            if (childScope) {
              childScope.$destroy();
              childScope = null;
            }
            if (block) {
              previousElements = getBlockNodes(block.clone);
              $animate.leave(previousElements).then(function() {
                previousElements = null;
              });
              block = null;
            }
          }
        });
           }

Now, it is my understanding that when you register a watcher in a 
directive/service/controller, you should capture its return and on the 
current scope's destroy method call it.

I would expect something like this (cut down for brevity):-
      var removeWatcher = $scope.$watch($attr.ngIf, function 
ngIfWatchAction(value) {
         ...
      } 
      $scope.$on('destroy', function destroy() { removeWatcher() });

I don't know if this is something I misunderstood or something born out 
ng-if being a special case born out of it being transclude: 'element'.

On Sunday, 27 September 2015 19:57:14 UTC+10, Sander Elias wrote:
>
> Hi Kamal,
>
> Here is a small plunk 
> <http://plnkr.co/edit/H6C06tnNY0xgIviGwDgA?p=preview> that shows you the 
> destroy is actually called inside an ngIf. Have a look at the console in 
> there, and toggle the button.
> The actual watcher on the ngIf itself is part of the scope that holds the 
> ngIf. Otherwise, it would only fire once.
>
> Regards
> Sander
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to