Consider this runnable example:
var app=angular.module('myapp', [])
.directive('mydct', function () {
return {
restrict:'EA',
transclude:true,
template:"<div>Template</div>",
replace:true,
scope:true,
link: function (scope, elm, attrs,ctrl,transcludeFn) {
transcludeFn( function( clone, scope ) {
console.log(clone[0]);
console.log(clone[1]);
console.log(clone[2]);
console.log(clone[3]);
//clone[3].attr('id');
});
}
};
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app='myapp'>
<mydct>
<div id='one'>This is one</div>
<span id='two'>This is two</span>
</mydct>
</body>
As you can see the clone parameter in the transcludeFunction will return the object as .contents(). This will include the text nodes. I have two questions:
- Is there a way of converting this contents array to a children array, thus omitting text nodes?
- As you can see, I will get out the text contents when accessing the elements in the array, what should I do to get them as elements, and be able to get the attribute?
This is a little verbose to emphasise the steps to go through, but feel free to simplify or shorten the code where necessary.