我不確定這是否實際可行,但我基本上想要在AngularJS中'&'隔離範圍的反向。這裏是一個Plunkr來演示。從transcluded內容訪問指令的隔離範圍
基本上,我有一個自定義指令設置,提供了一些可重用的HTML。它使用ng-transclude
來允許在其中呈現一些外部內容。但是,我發現了一種情況,我想從代碼的transcluded部分訪問已經在指令的隔離範圍中設置的函數。
所以基本上我有東西,看起來像:
<div ng-controller="MainController">
<!-- The directive -->
<div some-custom-directive>
<!-- Button 1 that invokes a method within the controller scope -->
<button id="button1" ng-click="invoke1()">Invoke from Controller</button>
<!-- Button 2 that invokes a method on the isolate scope for the custom directive -->
<button id="button2" ng-click="invoke2()">Invoke from Isolate Scope</button>
</div>
</div>
有誰知道,如果這確實是可能的嗎?
更新
按@馬克Rajcok的回答,對$scope
發現$$prevSibling
可用於從transcluded內容中訪問該指令的分離範圍。但是,我已經更新了上面的Plunkr,以便在ng-repeat
指令中嘗試執行此操作,但不起作用。我假設重複中的項目不會繼承範圍。
乾杯,工作。但是,我發現它不能在'ng-repeat'中工作(我假設每個項目都不會從控制器繼承'$ scope',反正每個項目都可以訪問'$$ prevSibling ''我已經更新了上面的Plunkr來舉個例子 – 2013-04-24 08:39:12
@SamuelSlade,看到我更新的答案(附圖) – 2013-04-25 03:23:44
輝煌,像魅力一樣工作!感謝您的圖表,非常豐富。 – 2013-04-25 09:07:36