admin管理员组文章数量:1298181
So I have created this checkbox directive that is made up of a label and input element. I want to set an id attribute on the directive and set this as the "id" of the input element and the label "for" attribute. This works fine, but the problem is that it remains on the outer html which is a div, so it breaks my directive. I thought that if you had the directive set to replace : true, then this would remove the html that defined the directive .
The directive is as follows
angular.module('journal.directives', []).
directive('fancyCheckbox', function(){
return {
restrict : 'EA',
replace : true,
template : '<div class="fancyCheckbox">' +
'<input type="checkbox" id="{{ id }}" />' +
'<label for="{{ id }}" ></label>' +
'</div>',
scope : {
colour : '@',
id : '@',
},
link : function(scope, elem, attrs){
var spotColourClass;
var valid_colours = ['blue', 'green', 'gray', 'purple',
'blue', 'orange', 'charcoal', 'light',
'yellow', 'red'];
if(valid_colours.indexOf(scope.colour) !== -1){
spotColourClass = scope.colour + "spot";
}else{
spotColourClass = 'greenspot';
}
elem.addClass(spotColourClass);
}
};
});
When I call my directive as
<fancy-checkbox colour="red" id="mycheckbox"></fancy-checkbox>
and it bees
<div class="fancyCheckbox ng-isolate-scope greenspot" colour="green" id="mycheckbox">
<input type="checkbox" id="mycheckbox">
<label for="mycheckbox"></label>
</div>
You see that the "id" is still on the outer div, which is not what I want. I assumed that this would be removed. So do I just have to remove it manually in the linking function
Hope someone can help
Thanks
So I have created this checkbox directive that is made up of a label and input element. I want to set an id attribute on the directive and set this as the "id" of the input element and the label "for" attribute. This works fine, but the problem is that it remains on the outer html which is a div, so it breaks my directive. I thought that if you had the directive set to replace : true, then this would remove the html that defined the directive .
The directive is as follows
angular.module('journal.directives', []).
directive('fancyCheckbox', function(){
return {
restrict : 'EA',
replace : true,
template : '<div class="fancyCheckbox">' +
'<input type="checkbox" id="{{ id }}" />' +
'<label for="{{ id }}" ></label>' +
'</div>',
scope : {
colour : '@',
id : '@',
},
link : function(scope, elem, attrs){
var spotColourClass;
var valid_colours = ['blue', 'green', 'gray', 'purple',
'blue', 'orange', 'charcoal', 'light',
'yellow', 'red'];
if(valid_colours.indexOf(scope.colour) !== -1){
spotColourClass = scope.colour + "spot";
}else{
spotColourClass = 'greenspot';
}
elem.addClass(spotColourClass);
}
};
});
When I call my directive as
<fancy-checkbox colour="red" id="mycheckbox"></fancy-checkbox>
and it bees
<div class="fancyCheckbox ng-isolate-scope greenspot" colour="green" id="mycheckbox">
<input type="checkbox" id="mycheckbox">
<label for="mycheckbox"></label>
</div>
You see that the "id" is still on the outer div, which is not what I want. I assumed that this would be removed. So do I just have to remove it manually in the linking function
Hope someone can help
Thanks
Share Improve this question asked Jan 4, 2014 at 16:38 kiznorekiznore 1592 gold badges4 silver badges13 bronze badges1 Answer
Reset to default 9From the Angular directive guide:
The replacement process migrates all of the attributes / classes from the old element to the new one.
So, what you're seeing is expected behavior.
Rather than remove the attribute in link I'd just create a new attribute, for instance check-id
and use it like this:
<fancy-checkbox colour="red" check-id="mycheckbox"></fancy-checkbox>
add it to your scope:
checkId : '@',
and use that within your template:
template : '<div class="fancyCheckbox">' +
'<input type="checkbox" id="{{ checkId }}" />' +
'<label for="{{ checkId }}" ></label>' +
'</div>',
demo fiddle
本文标签:
版权声明:本文标题:javascript - Creating angularjs directive with id attribute for internal html but id still on outer html - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741318990a2372091.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论