因爲你只需要它顯示的目的,而不是真正與綁定和其他東西使用它,在你面前的角度需要它已經觸及它。您可以使用指令的template
屬性的函數表達式語法。
實施例: -
var content;
template: function(elm){
content = elm.html(); //save the html
return '<div>Template</div>';
},
用於支撐多個擴展示例: -
.directive('myDirective', function($timeout) {
var content={};
var loadedDirs;
return {
scope:true,
template: function(elm){
loadedDirs = loadedDirs ||
angular.element(document.querySelectorAll('[my-directive]'));
//Save the element in its respective index
content[loadedDirs.index(elm)] = elm.html();
return '<div>Template<div><pre>{{orig}}</pre></div></div>'
},
link: function(scope, element) {
var idx = loadedDirs.index(element);
scope.orig = content[idx];
//Remove key
delete content[idx];
//Remove reference to loadedDirs
if(!Object.keys(content)){
loadedDirs = null;
}
}
};
});
Demo
否Jquery的溶液(工作周圍index
)
.directive('myDirective', function($timeout) {
var content={},
idx = 0;
return {
scope:true,
template: function(elm){
elm.idx = idx; //set an iterative index
//Save the element in its respective index
content[idx++] = elm.html(); //save element html in a map
return '<div>Template<div><pre>{{orig}}</pre></div></div>'
},
link: function(scope, element) {
var idx = element.idx; //Get idx property value from the element
scope.orig = content[idx]; //get the content
delete content[idx]; //remove the key
if(!Object.keys(content)){ idx = 0; } //reset idx once all are loaded
}
};
});
Demo
來源
2014-10-09 01:01:34
PSL
究竟是你想做些什麼?獲取輸入的值並放入div中? – PSL 2014-10-08 23:27:21
我想打印出指令的原始內容。真正的例子有點複雜,所以我試圖想出一個最小的例子。 – 2014-10-08 23:39:43
但最終你希望他們被替換嗎?在你的真實情況下它是否擁有大量的東西?你想對這些內容做任何事嗎? – PSL 2014-10-08 23:48:11