Having a controller, MyCtrl:
class MyCtrl {
constructor($scope, $rootScope, ...) {
this.$scope = $scope;
this.$rootScope = $rootScope;
this.doThis = _debounce(this.resize.bind(this), 300);
...
}
$onInit() { ... }
$onDestroy() { ... }
}
Inside $onInit is called $rootScope.$on. Like this one, is another controller with does the same operations but for a different type of page, let's say MyCtrl2. When I go to the second one from the first one, the first one keeps getting called because it is not destroyed. I solved this problem using the method described Prevent $rootScope.$on from calling function several times by adding a deregister.
The $onInit now:
$onInit() {
this.$rootScope.$on('toggleNav', () => {
this.doThis();
});
this.deregisterDoThis = this.$rootScope.$on('listen', function() {
this.$scope.doThis();
});
this.$rootScope.$on('$destroy', this.deregisterDoThis);
}
Now, if I go from the first controller's page to the page of the second, everything works fine. BUT, when I click for going back to the page of the first one, there is an error in the console saying:
Uncaught TypeError: Cannot read property 'getAttribute' of null at $.t.default [as attr] (vendor.dll.js?aca3f637cd46c07f3280:135) at attachNotes (VM133106 chart-util.js:1016) at invokeFunc (debounce.js?e59c:95) at trailingEdge (debounce.js?e59c:144) at timerExpired (debounce.js?e59c:132)
Any ideas about this?