7
我是新的角度。我想知道之前viewDashboard
控制器將卸載,我不知道我怎麼能檢測到。角度 - 當控制器被卸載時檢測到
Basicali單擊左側菜單中的項目Template.fn_change_view()
將Template.active_view
更改爲活動視圖名稱,例如, '儀表板',它顯示/隱藏一些view-
元素指令。
但是之後寫入非活動指令控制器的代碼不再執行。在此之前,我需要從這個不活動的控制器執行一個功能。有沒有辦法做到這一點?
我希望我寫得夠清楚,如果不是,我會盡力解釋得更好。
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>
左menu.html通過leftMenu指令加載
<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 - 由viewDashboard指令加載
<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指令
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'
};