它的好处是有主控制器的角度? [英] Is it good to have main controller in Angular?
问题描述
我不知道这是否是一个很好的做法......我在航线配置却因为定义控制器我的 HomeCtrl
是 NG-如果
语句他不能听 loginSuccess
所以我做了 MainCtrl
来监听 loginSuccess
并作出适当的反应。这code工作得很好,但是这闻起来像一个黑客给我。我应该删除 MainCtrl
并使其服务?如果是这样的一些例子是真是太好了。
的index.html
<机身NG-应用=对myAppNG控制器=MainCtrl>
< DIV NG-IF =isLoged()!>
<&登入GT;< /签到>
< / DIV>
< DIV NG-IF =isLoged()>
< DIV CLASS =头>
< DIV CLASS =导航>
< UL>
< A HREF =/><李班=书>的NavItem< /李>< / A>
< / UL>
< / DIV>
< / DIV>
< DIV CLASS =容器NG-视图=>< / DIV>
< / DIV>
< /身体GT;
App.js
angular.module('对myApp',[])
的.config(函数($ routeProvider){
$ routeProvider
。什么时候('/', {
templateUrl:意见/ main.html中,
控制器:'HomeCtrl
})
。除此以外({
redirectTo:'/'
});
})
.controller('MainCtrl',函数($范围){
$ scope.user = FALSE;
$ scope.isLoged =功能(){
如果($ scope.user){
返回true;
}其他{
返回false;
}
}
$ $范围在('事件:loginSuccess',函数(EV,用户){
$ scope.user =用户;
$ $范围适用于()。
});
})
.controller('HomeCtrl',函数($范围,$位置){
//这是家居控制器
})
.directive('签到',函数(){
返回{
限制:'E',
链接:功能(范围,元素,ATTRS){
//转到服务器,然后调用signinCallback();
}
};
})
.RUN(['$窗口','$ rootScope','$日志',函数($窗口,$ rootScope){
$ window.signinCallback =功能(RES){
如果(RES){
$ rootScope $广播('事件:loginSuccess',RES)。
}
其他{
$ rootScope $广播('loginFailure',RES)。
}
};
}]);
我开始我所有的角项目有:
< HTML NG-应用=APPNAMENG控制器=appNameCtrl>
使用一个全球性控制器的可能不是必要的,但它始终是好的,当需要时将它周围。比如,我用它在我的CMS设定有约束力的发起一切的加载 - 因此,所有的子控制器,因为它加载。那不是违反了关注点分离因为全球控制器的关注是促进其他控制器的负载。
这是说,只是一定要保持事物模块化/分离和可重复使用成为可能。如果你的控制器上,以功能依赖于全局控制器的存在,再有就是一个问题。
I dont know if this is a good practice... I have a controller defined in route config but because my HomeCtrl
is in ng-if
statement he cannot listen for loginSuccess
so I made MainCtrl
which listens for loginSuccess
and reacts appropriately. This code works just fine but this smells like a hack to me. Should I remove MainCtrl
and make it a service? If so some example would be really great.
Index.html
<body ng-app="myApp" ng-controller="MainCtrl">
<div ng-if="!isLoged()">
<signIn></signIn>
</div>
<div ng-if="isLoged()">
<div class="header">
<div class="nav">
<ul>
<a href="/"><li class="book">navItem</li></a>
</ul>
</div>
</div>
<div class="container" ng-view=""></div>
</div>
</body>
App.js
angular.module('myApp', [])
.config(function ($routeProvider) {
$routeProvider
.when('/', {
templateUrl: 'views/main.html',
controller: 'HomeCtrl'
})
.otherwise({
redirectTo: '/'
});
})
.controller('MainCtrl', function ($scope) {
$scope.user = false;
$scope.isLoged = function(){
if($scope.user){
return true;
}else{
return false;
}
}
$scope.$on('event:loginSuccess', function(ev, user) {
$scope.user = user;
$scope.$apply();
});
})
.controller('HomeCtrl', function ($scope, $location) {
//this is home controller
})
.directive('signIn', function () {
return {
restrict: 'E',
link: function (scope, element, attrs) {
//go to the server and then call signinCallback();
}
};
})
.run(['$window','$rootScope','$log',function($window, $rootScope){
$window.signinCallback = function (res) {
if(res){
$rootScope.$broadcast('event:loginSuccess', res);
}
else{
$rootScope.$broadcast('loginFailure',res);
}
};
}]);
I start all of my Angular projects with:
<html ng-app="appName" ng-controller="appNameCtrl">
The use of a "global" controller may not be necessary, but it is always nice to have it around when a need arises. For example, I use it in my CMS to set a binding that initiates the loading of everything else - so all the sub controllers are loaded because of it. That isn't violating separation of concerns because the global controller's concern IS to facilitate the loading of other controllers.
That said, just be sure to keep things as modular/separated and reusable as possible. If your controllers rely on the global controller's existence in order to function, then there is an issue.
这篇关于它的好处是有主控制器的角度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!