简体   繁体   English

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

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

I am creating a new template but right now I am using angular route, the problem is I use the top controller in all pages, but I would like that controller not appear in certain pages how I suppose to do? 我正在创建一个新模板,但是现在我正在使用角度路由,问题是我在所有页面中都使用了顶部控制器,但是我希望该控制器不出现在某些页面中,我应该怎么做?

html right now load all controllers route and the topCtrl, I would like that controller not run when the page is: html/clients-view.html html现在加载所有控制器路由和topCtrl,我希望该控制器在页面为html / clients-view.html时不运行

from this controller: 从此控制器:

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

angular: 角度:

 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: 的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>

You shouldn't try to load a "global" controller and than try to make some "special cases" where you don't want to load it. 您不应尝试加载“全局”控制器,而应尝试在不想加载的情况下进行“特殊情况”处理。 It doesn't make any sense especially if you have a second or third template in the future which also doesn't need this controller why do you load "topCtrl" than? 这没有任何意义,尤其是如果您将来有第二个或第三个模板,并且也不需要此控制器时,为什么要加载“ topCtrl”呢?

My advice would be to remove your topCtrl and create sort of provider or factory: 我的建议是删除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
            }])

You could also create some methods and inherit them to a object, for more detailed information I would need specific code 您还可以创建一些方法并将其继承给对象,有关更多详细信息,我需要特定的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM