简体   繁体   中英

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

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:

<!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?

My advice would be to remove your topCtrl and create sort of provider or factory:

...
.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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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