我可以通过多个控制器在$ routeProvider.when()在angularJS? [英] Can I pass multiple controllers in $routeProvider.when() in angularJS?
问题描述
我试图寻找这在不同的线程,但我不能断然明白这一点。
I tried searching for this on various threads, but I can't conclusively understand this.
test.config(['$routeProvider', function($routeProvider){
$routeProvider
.when('/',
{
controller:'SimpleController1',
templateUrl: 'partials/1.html'
})
.when('/xyz',
{
controller:'SimpleController1, SimpleController2',
templateUrl:'partials/2.html'
})
.otherwise({ redirectTo: '/'});
}]);
我试着做了上面的代码片段,但它不工作。我可以做这样的事情?如果是,那么它是什么,我做错了什么?
I tried doing the above snippet, but it's not working. Can I do something like this? If yes, then what is it that I'm doing wrong here?
推荐答案
只有一个控制器被允许,并在将被分配到装载模板作为控制器纳克视点
。无需在模板中定义 NG-控制器
的主控制器。
Only one controller is allowed and will be assigned to the loading template as the controller in ng-view
. No need to define ng-controller
in the template for a main controller.
如果您需要定义多个控制器我建议你定义一个主/父控制器和使用,在 routeProvider
,然后使用具有其他已经在模板中 NG-控制器
指令。
If you need to define multiple controllers I suggest you define one main/parent controller and use that in the routeProvider
and then have others already in the template using the ng-controller
directive.
或...
入住采用了棱角分明的用户界面的 UI-路由器: http://angular-ui.github.io/ 这是一种更灵活的路由器。
Check into using Angular UI's UI-Router : http://angular-ui.github.io/ which is a much more versatile router.
这篇关于我可以通过多个控制器在$ routeProvider.when()在angularJS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!