有没有办法将 class 添加到组件根元素?
Is there a way to add a class to the component root element?
除了在 html 中添加 class。
我的意思是这样的:
在html
<my-component></my-component>
在 js 中
angular.module('app').component('myComponent', {
template: '<div class="inner-element">Inner element</div>',
className: 'outer-element' <--- wanted property
});
这是我希望渲染后的样子:
<my-component class="outer-element"> <--- that's what I want to get
<div class="inner-element">Inner element</div>
</my-component>
您可以指定在组件 init
上添加 class 的控制器
controller: function($element) {
this.$onInit = function() {
$element.addClass('outer-element')
};
}
但这有点违背封装等。
除了在 html 中添加 class。
我的意思是这样的:
在html
<my-component></my-component>
在 js 中
angular.module('app').component('myComponent', {
template: '<div class="inner-element">Inner element</div>',
className: 'outer-element' <--- wanted property
});
这是我希望渲染后的样子:
<my-component class="outer-element"> <--- that's what I want to get
<div class="inner-element">Inner element</div>
</my-component>
您可以指定在组件 init
上添加 class 的控制器controller: function($element) {
this.$onInit = function() {
$element.addClass('outer-element')
};
}
但这有点违背封装等。