admin管理员组文章数量:1421549
Given
<mytag class="red">
<mysubtag id="3"></mysubtag>
</mysubtag>
subtag might have different meanings depending on the parent tag (it might be mytag or mysupercustomtag). I have directives for all parent tags. How can I access the subtags?
I know in the link function of the directive for mytag I can use children()
, contents()
, and data()
, and even elm.find('#someid')
. Would something like the following do the trick? What's the correct way of doing that? nested directives?
.directive('mytag', function() {
return {
restrict: 'A',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
// get the id of the first child (for example mysubtag)
var newid = element.children()[0].getAttribute('id');
// and put it in mytag, so that it is transformed into <div class="red" id="3">..
attrs.$set('id', newid);
}
}
})
Given
<mytag class="red">
<mysubtag id="3"></mysubtag>
</mysubtag>
subtag might have different meanings depending on the parent tag (it might be mytag or mysupercustomtag). I have directives for all parent tags. How can I access the subtags?
I know in the link function of the directive for mytag I can use children()
, contents()
, and data()
, and even elm.find('#someid')
. Would something like the following do the trick? What's the correct way of doing that? nested directives?
.directive('mytag', function() {
return {
restrict: 'A',
template: '<div></div>',
replace: true,
link: function (scope, element, attrs) {
// get the id of the first child (for example mysubtag)
var newid = element.children()[0].getAttribute('id');
// and put it in mytag, so that it is transformed into <div class="red" id="3">..
attrs.$set('id', newid);
}
}
})
Share
Improve this question
edited Aug 25, 2013 at 15:54
Eduard Gamonal
asked Apr 17, 2013 at 10:46
Eduard GamonalEduard Gamonal
8,0315 gold badges43 silver badges48 bronze badges
2
- I think as per angularjs, parents are not suppose to know about children – Arun P Johny Commented Apr 17, 2013 at 11:32
-
1
By the time the linking function runs,
mysubtag
is gone, since your directive has a template and usesreplace: true
. – Mark Rajcok Commented Apr 17, 2013 at 15:50
1 Answer
Reset to default 5This approach is not correct . For Nested directives you have to declare controller in parent directive and use require attribute in child directive to access parent controller.Please see below example from angular documentation for child parent directives. Below link will help you to set up one
http://angularjs/#create-ponents
本文标签: javascriptGet children attributes in a directiveStack Overflow
版权声明:本文标题:javascript - Get children attributes in a directive - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745352867a2654876.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论