[英]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.