この実行可能な例を考えてみましょう:
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 の clone パラメーターはオブジェクトを .contents() として返します。これには、テキスト ノードが含まれます。2 つの質問があります。
- このコンテンツ配列を子配列に変換して、テキスト ノードを省略する方法はありますか?
- ご覧のとおり、配列内の要素にアクセスするときにテキストの内容を取得しますが、それらを要素として取得し、属性を取得できるようにするにはどうすればよいでしょうか?