簡體   English   中英

Angular.js路由和控制器如何在某些位置刪除控制器

[英]Angular.js route and controllers how to remove controllers in certain places

我正在創建一個新模板,但是現在我正在使用角度路由,問題是我在所有頁面中都使用了頂部控制器,但是我希望該控制器不出現在某些頁面中,我應該怎么做?

html現在加載所有控制器路由和topCtrl,我希望該控制器在頁面為html / clients-view.html時不運行

從此控制器:

when('/clients/:link', {templateUrl: 'html/clients-view.html', controller: 'links_viewCtrl'}).

角度:

 angular.module('myApp', ['ngRoute','ngFileUpload'])
    .config(['$routeProvider',function($routeProvider) {
        $routeProvider.
        when('/', {templateUrl: 'html/home.html', controller: 'homeCtrl'}).
        when('/new_demo', {templateUrl: 'html/users.html', controller: 'usersCtrl'}).
        when('/clients/:link', {templateUrl: 'html/clients-view.html', controller: 'links_viewCtrl'}).

        otherwise({redirectTo: '/'});
    }])
    .controller('mainCtrl',['$scope','Status',function($scope,Status){
        $scope.Status = Status;
    }])

      .controller('topCtrl',['$scope','Status',function($scope,Status){
            $scope.Status = Status;
        }])
 .controller('links_viewCtrl',['$scope','Status',function($scope,Status){
                $scope.Status = Status;
            }])

的HTML:

<!doctype html>
  <html class="no-js" ng-app="myApp" lang="">
  <!--<![endif]-->
  <body ng-controller='mainCtrl'>
      <div id="navbar" class="navbar-collapse collapse" ng-controller="topCtrl"></div>

   <div class="container-fluid">
     <div ng-view=""></div>

     <footer>

     </footer>
  </div>
</body>
</html>

您不應嘗試加載“全局”控制器,而應嘗試在不想加載的情況下進行“特殊情況”處理。 這沒有任何意義,尤其是如果您將來有第二個或第三個模板,並且也不需要此控制器時,為什么要加載“ topCtrl”呢?

我的建議是刪除topCtrl並創建某種提供程序或工廠:

...
.factory('topFactory', ['Status', function (Status){
            var state = Status;
            return state;
        }]);
.controller('mainCtrl',['$scope','topFactory',function($scope,topFactory){
        $scope.Status = topFactory();
    }])
 .controller('links_viewCtrl',['$scope', function($scope){
                // do something
            }])

您還可以創建一些方法並將其繼承給對象,有關更多詳細信息,我需要特定的代碼

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM