Angular.js路由和控制器如何删除某些地方的控制器

时间:2015-10-08 15:06:03

标签: javascript angularjs

我正在创建一个新模板但是现在我正在使用角度路由,问题是我在所有页面中使用顶级控制器,但我希望控制器不会出现在某些页面中我想怎么做?

html现在加载所有控制器路由和topCtrl,我希望控制器在页面运行时不运行:html / clients-view.html

来自这个控制器的

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>

1 个答案:

答案 0 :(得分:0)

你不应该试图加载一个&#34;全球&#34;控制器,而不是尝试做一些特殊情况&#34;在哪里你不想加载它。它没有任何意义,特别是如果你将来有第二个或第三个模板,也不需要这个控制器为什么要加载&#34; topCtrl&#34;比λ

我的建议是删除你的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
            }])

你也可以创建一些方法并将它们继承到一个对象,更详细的信息我需要特定的代码

相关问题