0

我在與以下結構的模板(地址接觸-info.html裏):AngularJS:公開NG-重複範圍NG-transclude

<dd ng-repeat="contact in contactInfo | limitTo: displayedItems | orderBy:'desc'"> 
    <span ng-transclude></span> 
</dd> 

而且一個指令:

{ 
    templateUrl: 'address-contact-info.html', 
    restrict: 'A', 
    scope: { 
     contactInfo: '=' 
    }, 
    transclude: true, 
    link: function (scope, elem, attrs, ctrl, transclude) { 
     scope.displayedItems = 1; 
    } 
}; 

我想用這樣的指令:

<div address-contact-info contact-info="tel"> 
    <span>{{contact.number}}</span> 
</div> 

這意味着我要訪問我transcluded ELE內的NG-重複範圍但是我不知道該怎麼做。

你有什麼想法如何做到這一點,還是有更好的方法來做到這一點?

感謝您的幫助!

更新 這裏一個plunker:http://plnkr.co/edit/4DYEUhHAPuJOLyG9omCT

更新

我發現對我的作品,但我仍然有一個問題的解決方案。 我想在模板中跨越的元素有它們自己的指令,這些指令需要父指令。

現在,當我把我的linkFn內transcludeFn,我得到以下錯誤:

Error: [$compile:ctreq] Controller 'editableGroup', required by directive 'editable', can't be found! 

http://plnkr.co/edit/zriVyS7MzheSI8jcYM5d

在這種plunker我還沒有包括其他指令,但我得到一個相關transcludeFb錯誤也是如此。

Cannot set property 'nodeValue' of undefined 
+0

你有一個這樣的運動員嗎? – 2015-02-23 15:35:48

+0

你想要在指令模板而不是視圖中重複的任何原因? – isherwood 2015-02-23 15:37:50

+0

是的,因爲在模板中不僅僅是重複。 – Sebastian 2015-02-23 16:03:55

回答

0
<div address-contact-info contact-info="tel"> 
    <span>{{contact.number}}</span> 
</div> 

這個片段將與不具有參考聯繫控制器的範圍進行編譯。 因此ng-repeat不能顯示任何數據。

即使是transcluded內容也將與控制器的範圍一起編譯。不符合指令的範圍。

一種用於此的手段是: http://plnkr.co/edit/clrchL1HLtFQLZ4fctZx?p=preview

這可能不是正確的/適當的解決方案。

+0

謝謝,但我不能異步獲取模板時使用這種方法,對吧? – Sebastian 2015-02-24 07:38:21

+0

你可以試試這個.. http://stackoverflow.com/questions/28666974/not-able-to-add-attribute-in-directive-in-angular/28667449#28667449 – 2015-02-24 08:36:09