我可以在 angularJS 的 $routeProvider.when() 中传递多个控制器吗? [英] Can I pass multiple controllers in $routeProvider.when() in angularJS?

查看:16
本文介绍了我可以在 angularJS 的 $routeProvider.when() 中传递多个控制器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在各种线程上搜索此内容,但我无法最终理解这一点.

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-view 中的控制器.无需在主控制器的模板中定义 ng-controller.

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-controller 指令.

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.

或...

检查使用 Angular UI 的 UI-Router: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.

这篇关于我可以在 angularJS 的 $routeProvider.when() 中传递多个控制器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆