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

$onDestroy not called on Controller but $scope.$on($destroy) is #15073

Closed
@northkode

Description

@northkode

Having an issue with the $onDestroy of a Route controller not getting called.

class AccountController {

    constructor($state, $scope, $rootScope) {

        Object.assign(this, { $state,  $scope, $rootScope });

        this.watcher = $rootScope.$on('$stateChangeStart', (event, next, current) => {
            this.currentNavItem = next.url.split('/')[1];
        });

        // IS CALLED.. but looks like memory keeps leaking and doesn't actually destory anything
        $scope.$on('$destroy', () => { 
            this.watcher();
        })

    }

    $onInit() {
        console.log('init');
        this.currentNavItem = null
        this.currentNavItem = this.$state.current.url.split('/')[1];
    }

    // IS NOT CALLED
    $onDestroy() {
        console.log('destroy') 
        this.watcher();
    }

}

AccountController.$inject = ['$state', '$scope', '$rootScope']

export default AccountController;

I can't seem to get the $onDestroy to ever fire.. if i remove the inject of scope that doesn't help either..

I am using angular 1.5.8, this happens on all browsers.

Below is the routes file that i'm using.

This issue happens on all children routes as well.

routes.$inject = ['$stateProvider'];

export default function routes($stateProvider) {
  $stateProvider
    .state('account', {
      url: '/account',
      template: require('./template.html'),
      controller: 'AccountController',
      controllerAs: 'account',
      resolve: {}
  }).state('account.you', {
      url: '/you',
      template: require('./tabs/you.html'),
      controller: 'YouController',
      controllerAs: 'you',
      resolve: {}
  }).state('account.users', {
      url: '/users',
      template: require('./tabs/users.html'),
      controller: 'UsersController',
      controllerAs: 'users',
      resolve: {}
  }).state('account.groups', {
      url: '/groups',
      template: require('./tabs/groups.html'),
      controller: 'GroupsController',
      controllerAs: 'groups',
      resolve: {}
  }).state('account.surveys', {
      url: '/surveys',
      template: require('./tabs/surveys.html'),
      controller: 'SurveysController',
      controllerAs: 'surveys',
      resolve: {}
    })
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions