Angular - Detects when the controller is unloaded

I am new to angular. I want to know before the viewDashboard controller viewDashboard , and I'm not sure how I can detect this.

Basicali after clicking on an item from the menu on the left of Template.fn_change_view() changes Template.active_view to the active name of the view, for example. 'dashboard', and it shows / hides some directives of the view- element.

But after that, the code written inside the inactive directive controller no longer executes. Before this happens, I need to execute one function from this inactive controller. Is there any way to do this?

Hopefully I will write this clearly enough if I do not try to explain better.

HTML:

index.html

  <body class="template" ng-controller="TemplateController as Template" ng-class="{'on': Template.loged_in}"> <div class="mainFlexCont"> <left-menu class="menuFlexCont menu" ng-class="{'open': Menu.isMenuOpen}"></left-menu> <!-- dashboard is visible only when Template.active_view is equal to 'dashboard' --> <view-dashboard class="contentFlexCont" ng-if="Template.fn_active_view('dashboard')"></view-dashboard> <!-- ... --> </div> <!-- ... --> </body> 

left-menu.html loaded with the leftMenu directive

 <ul class="leftMenu"> <li> <a class="toggleLeft" ng-click="Menu.isMenuOpen = !Menu.isMenuOpen" href> <i class="fa fa-bars"></i> Toggle Menu </a> </li> <!-- view change by clicking one of list items --> <li ng-repeat="item in Menu.obj_items" ng-click="Template.fn_change_view(item.active)" ng-class="{'active' : Template.fn_active_view(item.active)}"> <a href> <i class="fa {{item.icon}}"></i> {{item.label}} </a> </li> </ul> 

dashboard.html - loaded by the viewDashboard directive

 <div class="row"> <div class="col-xs-12"> <h1>Dashboard</h1> </div> </div> <div class="row"> <div class="col-lg-7"> <chart-yearly class="chart"></chart-yearly> </div> </div> 

JS:

chartYearly directive

 app.directive('chartYearly', function () { return{ restrict: 'E', templateUrl: 'views/chart-yearly.html', controller: function ($scope) { // how I can detect here before Yearly controler will be unloaded? // when Template.active_view will change to different than dashboard $scope.$watch('Template.active_view', function () { // I need to execute this when this directive is active either before is unloaded console.log(2); }); }, controllerAs: 'Yearly' }; 
+7
javascript angularjs
source share
1 answer

Regions emit the $destroy event. See the events section in angular docs .

 $scope.$on('$destroy', function() { // clean up stuff }) 
+15
source share

All Articles