2014-12-28 21 views
7

Tôi mới ở góc cạnh. Tôi muốn biết trước khi viewDashboard bộ điều khiển sẽ tải xuống và tôi không chắc chắn cách tôi có thể phát hiện điều đó.Góc - phát hiện khi một bộ điều khiển được 'unloaded`

Cơ bản sau khi nhấp vào một mục từ menu bên trái Template.fn_change_view() thay đổi Template.active_view thành tên chế độ xem đang hoạt động, ví dụ: 'bảng điều khiển' và nó hiển thị/ẩn một số chỉ thị phần tử view-.

Nhưng sau đó mã được viết bên trong bộ điều khiển chỉ thị không hoạt động không thực thi nữa. Trước khi điều đó xảy ra, tôi cần thực thi một hàm từ bộ điều khiển không hoạt động này. Có cách nào để làm điều đó?

Tôi hy vọng tôi viết đủ rõ ràng, nếu không tôi sẽ cố gắng giải thích tốt hơn.

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>         

trái menu.html nạp bởi chỉ thị 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 - nạp bởi chỉ thị 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 chỉ

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' 
    }; 

Trả lời

15

Phạm vi phát ra một sự kiện $destroy. Xem phần sự kiện trong số angular docs.

$scope.$on('$destroy', function() { 
    // clean up stuff 
}) 
Các vấn đề liên quan