在嵌套路由中不调用子控制器的角度ui-路由器

时间:2023-01-13 19:39:56

I have my routes set up as below. Its too frustrating that the view in view.tab is loaded but its controller isn't called. I tried without the paramaters, but it still doesn't work as expected. Does anyone have any idea on how to solve this?

我的路线如下所示。看到这样的景象太令人沮丧了。已加载tab,但不调用它的控制器。我试过没有护理人员,但还是没有达到预期的效果。有人知道怎么解决这个问题吗?

    $stateProvider
    .state('index', {
        url: '/',
        templateUrl: viewsRoot + 'restaurants/index.htm',
        controller: 'RestaurantsCtrl'
    })
    .state('view', {
        url: '/view',
        controller: 'RestaurantsViewCtrl',
        templateUrl: viewsRoot + '/restaurants/view.htm'
    })
    .state('view.tab', {
        url: '/orders',
        // controller: 'OrdersIndexCtrl',
        controller: function ($scope) {
            alert('This does not run');
        },
        views: {
            "view": {
                templateUrl: viewsRoot + '/restaurants/orders.htm'
            }
        }
    });

$urlRouterProvider.otherwise("/");

1 个解决方案

#1


2  

You need to declare the controller along side the template:

您需要在模板旁边声明控制器:

views: {
            "view": {
                templateUrl: viewsRoot + '/restaurants/orders.htm',
                controller: 'MyController' // (or a function, etc.)
        }

The UI-Router wiki sort of alludes to this:

UI-Router也有类似的意思:

If you define a views object, your state's templateUrl, template and templateProvider will be ignored. So in the case that you need a parent layout of these views, you can define an abstract state that contains a template, and a child state under the layout state that contains the 'views' object.

如果定义一个视图对象,则会忽略状态的templateUrl、template和templateProvider。因此,如果您需要这些视图的父视图,您可以定义一个包含模板的抽象状态,以及包含“视图”对象的布局状态下的子状态。

Controllers are paired with a view. So if it ignores the "template" properties defined on the state, it seems to imply that it will ignore the controller too.

控制器与视图配对。因此,如果它忽略了在状态上定义的“模板”属性,它似乎意味着它也将忽略控制器。

If you want all of your named views to share a controller, define an abstract parent state as the wiki suggests.

如果您希望所有命名视图共享一个控制器,请按照wiki的建议定义一个抽象父状态。

#1


2  

You need to declare the controller along side the template:

您需要在模板旁边声明控制器:

views: {
            "view": {
                templateUrl: viewsRoot + '/restaurants/orders.htm',
                controller: 'MyController' // (or a function, etc.)
        }

The UI-Router wiki sort of alludes to this:

UI-Router也有类似的意思:

If you define a views object, your state's templateUrl, template and templateProvider will be ignored. So in the case that you need a parent layout of these views, you can define an abstract state that contains a template, and a child state under the layout state that contains the 'views' object.

如果定义一个视图对象,则会忽略状态的templateUrl、template和templateProvider。因此,如果您需要这些视图的父视图,您可以定义一个包含模板的抽象状态,以及包含“视图”对象的布局状态下的子状态。

Controllers are paired with a view. So if it ignores the "template" properties defined on the state, it seems to imply that it will ignore the controller too.

控制器与视图配对。因此,如果它忽略了在状态上定义的“模板”属性,它似乎意味着它也将忽略控制器。

If you want all of your named views to share a controller, define an abstract parent state as the wiki suggests.

如果您希望所有命名视图共享一个控制器,请按照wiki的建议定义一个抽象父状态。