简体   繁体   English

UI路由器的父状态,子状态控制器

[英]Ui-router parent state, child states controllers

I'm trying to create a parent state which should go to a childstate by default. 我正在尝试创建一个父状态,默认情况下应转到子状态。 I need to be able to have a parentController and ChildControllers. 我需要能够有一个parentController和ChildControllers。 I tried the following but as you can see my childControllers are not called. 我尝试了以下操作,但是您可以看到未调用childControllers。

var app = angular.module('test', ['ui.router']);

app.config(config);

app.controller('ParentCtrl', ['$scope', function ($scope) {
    console.log('parentCtrl');
}]);

app.controller('Child1Ctrl', ['$scope', function ($scope) {
    console.log('Child1Ctrl');
}]);

app.controller('Child2Ctrl', ['$scope', function ($scope) {
    console.log('Child2Ctrl');
}]);

states: 状态:

function config($stateProvider, $urlRouterProvider) {
    $urlRouterProvider.otherwise('/parent');

    $stateProvider.state('parent', {
        abstract: true,
        views: {
            'main': {
                controller: 'ParentCtrl',
                controllerAs: 'parentCtrl',
                templateUrl: 'page.html'
            }
        },
        url: '/parent/'
    });
    $stateProvider.state('parent.child1', {
        views: {
            'main': {
                controller: 'Child1Ctrl',
                controllerAs: 'child1Ctrl',
                templateUrl: 'page.html'
            }
        },
        url: 'child1?param1',
    });
    $stateProvider.state('parent.child2', {
        views: {
            'main': {
                controller: 'Child2Ctrl',
                controllerAs: 'child2Ctrl',
                templateUrl: 'page.html'
            }
        },
        url: 'child2?param2', //is this /parent/child2... ?
    });
}

You can also find the code here: https://jsfiddle.net/vjgh8ntL/2/ 您也可以在这里找到代码: https : //jsfiddle.net/vjgh8ntL/2/

Can someone guide me in the right direction? 有人可以指引我正确的方向吗?

I would use in this case just different otherwise: 在这种情况下,我将使用其他方式:

$urlRouterProvider.otherwise('/parent/child1?child1=null');

Check this working example 检查此工作示例

There are similar Q & A (with even different solutions, eg .when() setting) : 有类似的问答(甚至有不同的解决方案,例如.when()设置)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM