I have the following HTML:
<my-dom attri="something 01">content 01</my-dom>
<my-dom attri="something 02">content 02</my-dom>
<my-dom attri="something 03">content 03</my-dom>
And now I want this to be replaced in runtime with:
<div>something 01: content 01</div>
<div>something 02: content 02</div>
<div>something 03: content 03</div>
Therefore I created this AngularJS directive:
app.directive('myDom', function() {
var content;
return {
restrict: 'EA',
replace: true,
scope : true,
// scope : false,
// scope: {},
template: function(elem, attr){
content = elem.clone();
return '<div>{{myContent}}: {{oldContent}}</div>';
},
controller: function(){
},
link: {
pre: function(scope, elem, attr){
scope.myContent = content.attr("attri"); // could also use "attr"
scope.oldContent= content.html(); // could not use "elem" or "attr"
},
post: function(scope, elem, attr){
},
}
};
});
Here I first clone the originale DOM element in the private variable "content" and then replacing the DOM element with the new one. In this new HTML Tag I insert the attribute data from the old code. But here it's getting mixed up:
The output code is:
<div>something 03: content 03</div>
<div>something 03: content 03</div>
<div>something 03: content 03</div>
So there is something wrong with the scope. But what?
From the documentation the scope : true
is creating a new scope for the directive by inheriting the parent scope.
Changes in the parent scope does affect the directive scope but changes in the directive scope does NOT change the parent scope.
The only thing I can imagine:
This one directive only has one scope. So all three usages of <my-dom>
share one and the same scope.
Is that correct? And how can I manage to get the result I want?