我已经注册了一个州test
,其中有一个孩子.child1
,这里的父母(test
)工作正常。当我导航到状态test.child1
时,网址会更改为#/test/child1
,但视图保持不变,子模板无法正常工作
angular.module('uiRouterSample.contacts', [
'ui.router'
])
.config(
[ '$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('test',{
url:'/test',
template:'<a ui-sref="test">Parent</a> -> <a ui-sref=".child1">child1</a>',
controller: ['$scope', '$state', 'contacts', 'utils',
function ( $scope, $state, contacts, utils) {
}]
}).state('test.child1',{
url:'/child1',
template:'Child template working fine'
})
}
]
)
答案 0 :(得分:14)
在父状态模板(测试状态)中需要ui-view
指令:
template:'<div ui-view/> <a ui-sref="test">Parent</a> <a ui-sref=".child1">child1</a></div>',
基本上每当你在ui-router中有一个具有子状态的状态时,那个子状态的直接父pf也必须在其模板中有一个ui-view
指令。
答案 1 :(得分:3)
发布此答案,因为我没有足够的代表发表评论。正如Mohammad所提到的,在父状态的模板中需要ui-view
。 Index html中的ui-view
只允许您呈现父状态(test
状态),但是为了呈现.child1状态,它需要在其父状态内有另一个ui-view
,在这种情况下是test
状态。
因此,请将test
州的模板配置为包含ui-view
:
angular.module('uiRouterSample.contacts', ['ui.router'])
.config(['$stateProvider', '$urlRouterProvider',
function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('test',{
url:'/test',
template:'<div ui-view/> <a ui-sref="test">Parent</a> -> <a ui-sref=".child1">child1</a></div>',
controller: ['$scope', '$state', 'contacts', 'utils',
function ( $scope, $state, contacts, utils) {
}]})
.state('test.child1',{
url:'/child1',
template:'Child template working fine'
})
}]