Can I pass multiple controllers in $routeProvider.when() in angularJS?

↘锁芯ラ 提交于 2019-12-17 19:03:33

问题


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?


回答1:


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.

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.

or...

Check into using Angular UI's UI-Router : http://angular-ui.github.io/ which is a much more versatile router.



来源:https://stackoverflow.com/questions/25061540/can-i-pass-multiple-controllers-in-routeprovider-when-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!