2016-06-07 123 views
1

我試圖創建一個使用UI-RouterAngularjs UI-Router:如何在嵌套視圖內創建視圖? (3級嵌套視圖)

代碼

.state('discussions', { 
      url: '/discussions', 
      views: { 
       '': { 
        templateUrl: 'views/groups/groupBaseView.html' 
       }, 
       '[email protected]': { 
        templateUrl: 'views/groups/groupView.html', 
        controller: 'groupController', 
        views: { 
         '[email protected]@discussions': { 
          template: 'hi' 
         } 
        } 
       } 
      } 

    }); 

你可以想像像

--groupBase 
--groupBase/groupView 
--groupBase/groupView/discussionView 

的視圖的嵌套視圖內視圖UI工作正常,直到--groupBase/groupViewdisucussionView沒有得到加載groupView

回答

1

a worknig plunker

查看嵌套可能,一個國家的內部。該國家views: {}設置必須包含所有這些觀點:

.state('discussions', { 
    url: '/discussions', 
    views: { 
    '': { 
     templateUrl: 'views/groups/groupBaseView.html' 
    }, 
    '[email protected]': { 
     templateUrl: 'views/groups/groupView.html', 
     controller: 'groupController', 
    }, 
    '[email protected]': { 
     template: '<h3>hi from right container</h3>' 
    }, 
    } 

}); 

到位到這一點,我們就可以實現視圖嵌套

  • 第一視圖(的意見/組/ groupBaseView.html ')被注入到未命名的UI視圖裏面index.html
  • 第二視圖( '視圖/組/ groupView.html')被注入的意見/組/ groupBaseView.html'
  • 第三轉到的意見/組/ groupView.html'

所以,'views/groups/groupView.html'看起來就像這樣:

<div> 
    ... 
    <div ui-view="leftcontainer"></div> 
</div> 

'views/groups/groupView.html'將是:

<div> 
    ... 
    <div ui-view="rightcontainer"></div> 
</div> 

檢查它here

+1

非常感謝您讓這麼複雜的時間變得如此簡單。我讀了你的答案,然後自己試了一下,一切都變得如此順利。 http://plnkr.co/DcsBaPCyqV5p8waklGda –

0

您不能擁有嵌套的「視圖」屬性。你必須首先定義一個孩子的狀態,然後定義它的視圖。

一個簡單的例子:

.state("a", { 
    views: { 
     template:"<ui-view></ui-view>" 
    } 
}) 
.state("a.b", { 
    views: { 
     template:"<ui-view></ui-view>" 
    } 
}) 
.state("a.b.c", { 
    views: { 
     template:"<h1>Something</h1>" 
    } 
}) 
+0

如何實現3個或更多層次的'ui-views'? –