我正在使用ui-router来嵌套状态和视图.当我单击链接时,URL将更改为子状态的URL,但模板不会加载.
例如,当URL更改为子状态时project/settings
,project.settings.html
不会加载相应的模板.
这是一个礼貌SSCCE Plunkr
以下是我的代码:
myApp.config(function ($stateProvider, $urlRouterProvider){ $stateProvider .state('project', { url: "/project", views:{ "content":{templateUrl: "partials/project.html"}, "header":{templateUrl: "partials/header"} } }) .state('project.settings', { url: "/settings", views:{ "content":{templateUrl: "partials/project.settings.html"}, "header":{templateUrl: "partials/header"} } }) $urlRouterProvider.otherwise("/") }); /* cheching whether the user is authentic or not*/ myApp.run(function($rootScope,$location,$cookieStore,validateCookie,$state) { $rootScope.$on('$stateChangeStart',function(event,toState,toParams,fromState){ if($cookieStore.get('user')){ validateCookie.authService(function(result,error){ if(!result){ $location.path("/"); }else{ $state.go('project.settings'); } }); } else{ $location.path("/"); } }); });
Project Setting -State test
Vladyslav Ba.. 23
嵌套的project.settings状态需要使用'@'后缀明确地处理更高状态的视图,即:
.state('project.settings', { url: "/settings", views:{ "content@":{templateUrl: "partials/project.settings.html"}, "header@":{templateUrl: "partials/header"} } })
在这里查看更多详细信息https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views#view-names---relative-vs-absolute-names
嵌套的project.settings状态需要使用'@'后缀明确地处理更高状态的视图,即:
.state('project.settings', { url: "/settings", views:{ "content@":{templateUrl: "partials/project.settings.html"}, "header@":{templateUrl: "partials/header"} } })
在这里查看更多详细信息https://github.com/angular-ui/ui-router/wiki/Multiple-Named-Views#view-names---relative-vs-absolute-names
首先将project.settings.html
templateurl和文件名中的文件名更改为projectSettings.html
(删除点).
.state('project.settings', { url: "/settings", views:{ "content":{templateUrl: "partials/projectSettings.html"}, "header":{templateUrl: "partials/header"} } })
在项目模板中添加两个div来加载子页面(标题abd projectSettings)
注意:此div中的任何内容都将替换为此处加载的页面.
project.html
<div ng-controller="userController"> <div class="details"> <a ui-sref=".settings" class="btn btn-primary">Settings</a> </div> <div ui-view="header">( Project Page header will replace with projectSettings header )</div> <div class="container" ui-view="content">( Project Page content will replace with projectSettings Content )</div> </div>