我正在使用angular-ui-bootstrap tabs和angular-ui-router。我想在點擊標籤時將特定狀態加載到標籤窗格中。這工作正常。問題在於狀態被加載到DOM中的每個選項卡窗格中。儘管用戶永遠無法說出,但是在DOM中不必要地重複所有這些內容並不是一個好主意。爲什麼發生這種情況的原因很明確 - 國家模板被插入到用戶界面視圖,如可以看到下面:angular-ui-router和angular-ui-bootstrap選項卡:多次將內容加載到DOM中
<tabset>
<tab class="clinical-tabs" ng-repeat="tab in tabs" heading="{{tab.title}}" active="tab.active" disabled="tab.disabled" ui-sref="activity.clinical.{{tab.title}}">
<div ui-view></div>
</tab>
</tabset>
,其結果是,該內容被加載在每個選項卡窗格:
<div class="tab-content">
<!-- ngRepeat: tab in tabs -->
<div class="tab-pane ng-scope active" ng-repeat="tab in tabs" ng-class="{active: tab.active}" tab-content-transclude="tab">
<!-- uiView: -->
<div ui-view="" class="ng-scope">
CONTENT IS LOADED HERE
</div>
</div>
<!-- end ngRepeat: tab in tabs -->
<div class="tab-pane ng-scope" ng-repeat="tab in tabs" ng-class="{active: tab.active}" tab-content-transclude="tab">
<!-- uiView: -->
<div ui-view="" class="ng-scope">
CONTENT IS ALSO LOADED HERE
</div>
</div>
<!-- end ngRepeat: tab in tabs -->
...
...
...
</div>
就像我說的,問題的原因很明顯。但是,我沒有足夠的經驗與Angular知道如何提出解決方案。
感謝您的建議。不幸的是,這隻適用於第一個標籤。如果我點擊任何其他標籤,則不會加載任何內容。點擊時,我需要爲每個標籤加載相應的內容(例如,轉到狀態):例如,單擊第一個標籤並將其模板注入到相應的ui視圖中。目前,它將模板注入所有的ui視圖(如果我有5個選項卡,它將被注入DOM 5次)。 – paul 2014-08-29 20:42:53
啊,我誤解了。在這種情況下,您可能要查找的實際上是['ngInclude'](https://docs.angularjs.org/api/ng/directive/ngInclude) – jdotjdot 2014-08-29 20:58:35
我無法確定ngInclude如何解決問題。它可能,但我看不到它。無論如何,我找到了一個很好的解決方案(查看發佈的答案)。 – paul 2014-08-30 15:19:19