是否有任何方法將某些內容轉換爲指令而不添加額外的元素。Transclude在AngularJS中不添加新元素
例如
指令:
{
scope: {
someParam: "="
},
link: function(scope, element, attrs){
//do something
},
transclude: true,
template:'<div ng-transclude></div>'
}
HTML源文件:
<div my-directive some-param="somethingFromController">
my transcluded content: {{somethingElseFromController}}
</div>
有了這個例子中的額外的div被添加到標記。通常這會很好,但我試圖在表內使用這個指令,所以添加一個div標籤會把事情搞砸。
我也試過沒有指定transclude
或template
,它擺脫了額外的div標籤,但現在{{somethingElseFromController}}
無法找到,因爲「transcluded」內容處於隔離範圍。我知道我可以從鏈接函數中的attrs對象獲取指令的參數,而不是創建一個獨立的作用域,但我寧願避免使用作用域來評估字符串$ apply()。
任何人都知道如何做到這一點? 謝謝!
什麼是「transcluded」內容'somethingElseFromController'是意味着提供? –
可能是任何東西。例如來自控制器的字符串。我只需要將我的transcluded內容與控制器放在同一個作用域中。 – rob
角需要一個元素來'掛起'範圍。但是,如果您只是想添加行爲,那麼正如您所說的那樣,您不需要模板。是的,否則,請使用父範圍。 –