javascript-在Angular ui路由器中嵌套状态url更改,但模板未加载

我正在使用ui-router来嵌套状态和视图。 当我单击链接时,URL会更改为该子状态的URL,但不会加载模板。

例如,当URL更改为子状态project/settings时,相应的模板project.settings.html未加载。

这是由Plunker提供的SSCC

下面也是我的代码:

app.js

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("/"); 
         }
        });
    });

index.html

                <div ng-controller="userController">
                    <div ui-view="header"></div>
                    <div class="container" ui-view="content"></div>
                </div>

project.html

            <div ng-controller="userController">
                <div class="details">
                    <a ui-sref=".settings" class="btn btn-primary">Settings</a>
                </div>
            </div>

project.settings.html

        <h1>Project Setting -State test</h1>
6个解决方案
23 votes

您的嵌套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]

Vladyslav Babenko answered 2020-06-30T02:26:51Z
20 votes

首先,将templateurl中的文件名project.settings.html和文件名更改为projectSettings.html(删除点)。

   .state('project.settings', {
         url: "/settings",
          views:{
               "content":{templateUrl: "partials/projectSettings.html"},
               "header":{templateUrl: "partials/header"}
           }
    }) 

在项目模板中添加两个div以加载子页面(标题和项目设置)

注意:此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>
Sajin M Aboobakkar answered 2020-06-30T02:27:24Z
3 votes

我有同样的问题,解决方案是将ui-view放到您父母的模板中。 因为您的局部视图也同样需要,因此如果它们将从子状态加载模板。见这里[https://github.com/angular-ui/ui-router/wiki/Frequently-Asked-Questions#issue-my-templates-are-not-appearing--loading--showing]

Demven Weir answered 2020-06-30T02:27:44Z
2 votes

如果将嵌套视图与UI-router一起使用,请确保将ui-view添加到父页面的html中,并包括特定的命名视图。

Thinkerer answered 2020-06-30T02:28:04Z
1 votes

我有同样的问题,解决方案是; 打开project.html文件(即父页面),然后将所有内容包装在<ui-view>

因此,替换这些:

<div ng-controller="userController">
  <div class="details">
    <a ui-sref=".settings" class="btn btn-primary">Settings</a>
  </div>
</div>

用这些:

<ui-view>
 <div ng-controller="userController">
   <div class="details">
     <a ui-sref=".settings" class="btn btn-primary">Settings</a>
   </div>
 </div>
</ui-view>

这样您就可以走了;)

Must Ckr answered 2020-06-30T02:28:37Z
0 votes

使用ui-sref="project.settings"作为project.html模板中的链接。

coquin answered 2020-06-30T02:28:57Z
translate from https://stackoverflow.com:/questions/21424524/in-angular-ui-router-nested-state-url-changes-but-template-is-not-loading