在Angular ui-router子模板中不起作用

时间:2021-08-06 19:40:34

I have registered a state test with a child .child1 , here parent (test) working fine . when i navigate to state test.child1 URL gets changed to #/test/child1 but the view remains same , child template not working

我已经注册了一个孩子.child1状态测试,这里父母(测试)工作正常。当我导航到状态test.child1 URL变为#/ 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'
        })
    }
  ]
)        

2 个解决方案

#1


14  

You need a ui-view directive in the template of the parent state (the test state):

您需要在父状态模板(测试状态)中使用ui-view指令:

  template:'<div ui-view/> <a ui-sref="test">Parent</a>  <a ui-sref=".child1">child1</a></div>',

Basically whenever you have a state in ui-router which will have child states, the immediate parent pf that child state must also have a ui-view directive in its template.

基本上,每当你在ui-router中有一个具有子状态的状态时,那个子状态的直接父pf也必须在其模板中有一个ui-view指令。

#2


3  

Posting this as answer as I do not have enough reps to comment. As mentioned by Mohammad, you need a ui-view inside the template of the parent state. The ui-view in your Index html only allows you to render the parent state(test state), but in order for .child1 state to be rendered, it needs another ui-view inside its parent state, which in this case is the test state.

发布此答案,因为我没有足够的代表评论。正如*所提到的,你需要在父状态模板中使用ui-view。您的Index html中的ui-view只允许您呈现父状态(测试状态),但是为了呈现.child1状态,它需要在其父状态内的另一个ui-view,在这种情况下是测试州。

Thus, configure your test state's template to contain a ui-view:

因此,配置测试状态的模板以包含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'
    })
   }]

#1


14  

You need a ui-view directive in the template of the parent state (the test state):

您需要在父状态模板(测试状态)中使用ui-view指令:

  template:'<div ui-view/> <a ui-sref="test">Parent</a>  <a ui-sref=".child1">child1</a></div>',

Basically whenever you have a state in ui-router which will have child states, the immediate parent pf that child state must also have a ui-view directive in its template.

基本上,每当你在ui-router中有一个具有子状态的状态时,那个子状态的直接父pf也必须在其模板中有一个ui-view指令。

#2


3  

Posting this as answer as I do not have enough reps to comment. As mentioned by Mohammad, you need a ui-view inside the template of the parent state. The ui-view in your Index html only allows you to render the parent state(test state), but in order for .child1 state to be rendered, it needs another ui-view inside its parent state, which in this case is the test state.

发布此答案,因为我没有足够的代表评论。正如*所提到的,你需要在父状态模板中使用ui-view。您的Index html中的ui-view只允许您呈现父状态(测试状态),但是为了呈现.child1状态,它需要在其父状态内的另一个ui-view,在这种情况下是测试州。

Thus, configure your test state's template to contain a ui-view:

因此,配置测试状态的模板以包含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'
    })
   }]