Assign multiple controller in $stateProvider.state

后端 未结 2 2104
别跟我提以往
别跟我提以往 2021-02-07 15:20

Probably this is an easy question for advanced angular users, but I did not find this issue somewhere well explained.

So I was restructuring my code, when I realized, I

2条回答
  •  清酒与你
    2021-02-07 16:05

    Syntaxically, it can't work. This (syntaxically) could work :

    $stateProvider.state('a.view', {
        url: "/anurl",
        views: {
            'menuContent': {
                templateUrl: "anUrlToMyTemplates",
                controller: ['ACtrl', 'BCtrl']
            }
        }
    }); 
    

    But AngularJS use ZERO or ONE controller by DOMElement.

    You can assign CtrlA for your A view :

    $stateProvider.state('a.view', {
        url: "/anurl",
        views: {
            'menuContent': {
                templateUrl: "anUrlToMyTemplates",
                controller: 'ACtrl'
            }
        }
    }); 
    

    And then into your A view :

    That said, for code design purpose, the correct way is to merge the actions of your two controllers in only one if they have to control the same template elements. If they control different parts of the template, use one controller for one part, or a controller for the whole view, and an other for the specific part :

    
    

提交回复
热议问题