考虑这个可运行的例子:
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>
如您所见,transcludeFunction 中的克隆参数将对象作为 .contents() 返回。这将包括文本节点。我有两个问题:
- 有没有办法将此内容数组转换为子数组,从而省略文本节点?
- 如你所见,我在访问数组中的元素时会取出文本内容,我该怎么做才能将它们作为元素获取,并且能够获取属性?