gpt4 book ai didi

javascript - Ui-router父状态,子状态 Controller

转载 作者:行者123 更新时间:2023-11-27 23:51:38 25 4
gpt4 key购买 nike

我正在尝试创建一个默认情况下应进入子状态的父状态。我需要能够有一个parentController 和ChildControllers。我尝试了以下操作,但如您所见,我的 childController 没有被调用。

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');
}]);

状态:

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... ?
});
}

您还可以在这里找到代码:https://jsfiddle.net/vjgh8ntL/2/

有人可以引导我走向正确的方向吗?

最佳答案

在这种情况下我会使用不同的否则:

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

检查这个working example

有类似的问答(甚至有不同的解决方案,例如.when()设置):

关于javascript - Ui-router父状态,子状态 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32651810/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com