Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

SCOPE DESTRUCTION: Directive scope is never destroyed if immediately switched out and it has templateUrl. #9197

Closed

Description

@applegrew

We have a directive which uses templateUrl that is rendered within a ngSwitch. There is a scenario where on page load the directive is rendered, but then immediately some conditions change causing ngSwitch to remove the directive from page. I found that in that case the directive's scope never gets destroyed.

A Plunker demo is available here - http://plnkr.co/edit/QYnUKQrHA4uivJifsYne?p=preview

In the demo open browser's JS console. Notice that you will only see '>>>Created' message, but no '>>>Destroyed'. This bug happens only when the directive uses templateUrl. If you change the above demo code to use template instead then notice the bug is gone.

The html snippet is:-

<body ng-controller="Main">
<div>
  <div ng-switch="code">
    <div ng-switch-when="red">
      <red></red>
    </div>
  </div>
</div>
</body>

The controller code is:-

controller('Main', function ($scope, $timeout) {
  $scope.code = 'red';
  $timeout(function () {
    $scope.code = 'blue';
  })
})

So as soon as the controller loads, $scope.code is set to 'blue' in next digest cycle, causing the directive red to appear momentarily.

The directive code is:-

directive('red', function () {
  return {
    restrict: 'E',
    replace: true,
    templateUrl: 'red.html',
    scope: {},
    link: function (scope) {
      console.log('>>>Created');
      scope.$on('$destroy', function (event) {
        console.log('>>>Destroyed');
      })
    }
  };
})

If I use template instead of templateUrl, then $destroy is called. The replace: true or isolated scope are not necessary to replicate this bug.

Activity

  1. lgalfaso commented on Sep 21, 2014

    @lgalfaso
    Contributor

    The issue is that the scope that the directive gets is already destroyed, in fact, it is already removed from the hierarchy

  2. lgalfaso commented on Sep 21, 2014

    @lgalfaso
    Contributor

    Let me take a deeper look into this issue

  3. lgalfaso commented on Sep 21, 2014

    @lgalfaso
    Contributor

    it looks like this is a bigger issue than expected

  4. added this to the 1.3.0-rc.4 milestone on Sep 24, 2014
  5. caitp commented on Sep 24, 2014

    @caitp
    Contributor

    @petebacondarwin assigning this to you since it's basically the same as lucas' dupe (lets just close it when we land the fix)

  6. changed the title [-]Directive scope is never destroyed if immediately switched out and it has templateUrl.[/-] [+]SCOPE DESTRUCTION: Directive scope is never destroyed if immediately switched out and it has templateUrl.[/+] on Sep 26, 2014
  7. added 2 commits that reference this issue on Sep 29, 2014
    3c8c88e
    5c9c197
  8. tiff commented on Oct 5, 2014

    @tiff

    The Changelog says that this also made it into v1.2.26. I just downloaded it from angularjs.org and didn't find this change in it...

    Edit: Probably because of this commit

  9. cztomsik commented on Oct 21, 2014

    @cztomsik

    +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions