I have the simple directive:
.directive('myDirective', function () {
restrict: 'E',
replace: true,
transclude: true,
scope: {
label: '@',
ngModel: '=',
class: '@'
},
template: "<div><span>{{label}}</span><input class='{{class}}' type='text' ng-model='ngModel' /></div>"
}
Using:
<myDirective label="myLabelA myClassB" class="my-class" ng-model="myModel" />
In result looks like:
<div label="myLabelA myClassB" class="my-class" ng-model="myModel">
<span>myLabel</span>
<input class="myLabelA myClassB" type="text" ng-model="myModel" />
</div>
Is there a way to eliminate the attributes from the root div element?
You can set replace
to false, then HTML will contain original my-directive
element and new element inside of it, without attributes of parent.
http://plnkr.co/edit/Vdw3YVMwzgElCFF7EGeM?p=preview
If you need to remove back-effects of existence DOM element - rename attribute class
of directive, so browser will not apply it to parent element.
You can remove these attributes by providing a compile function to the directive that removes those attributes from the element:
.directive('myDirective', function () {
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {
label: '@',
ngModel: '=',
class: '@'
},
template: "<div><span>{{label}}</span><input class='{{class}}' type='text' ng-model='ngModel' /></div>",
compile: function ($element) {
$element.removeAttr('label')
.removeAttr('class')
.removeAttr('ng-model');
}
}
})
regards
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.