简体   繁体   English

如何引用Angular2组件的CSS中的host元素?

[英]How do I reference host element in CSS of an Angular2 component?

I have a component in Angular2 and I want to set the padding on the host element. 我在Angular2中有一个组件,我想在主机元素上设置填充。

@Component({
    selector: 'my-app',
    template: 'Hello world'
    styles: ['my-app {padding: 10px}']
})

in browser, my style looks like this: 在浏览器中,我的风格如下所示:

my-app[_ngcontent-icu-3] {padding: 10px;}

while my host element looks like this: 而我的主机元素看起来像这样:

<my-app _ngcontent-icu-1="" _nghost-icu-3="">
    <div class="content" _ngcontent-icu-3=""></div>
</my-app>

As I understand it, Angular2 adds these _ng attributes so CSS references specific components. 据我了解,Angular2添加了这些_ng属性,因此CSS引用了特定的组件。

But how do I reference the host element from the CSS defined inside the component? 但是如何从组件内定义的CSS引用主机元素?

styles: [':host {padding: 10px}']

or 要么

@Component({
    selector: 'my-app',
    template: 'Hello world'
    host: {'[style.padding.px]': '"10"'}
})

The added _ng... classes are for emulating scoped styles (default encapsulation: ViewEncapsulation.Emulated ) similar to what shadow DOM does. 添加的_ng...类用于模拟范围样式(默认encapsulation: ViewEncapsulation.Emulated ),类似于DOM DOM。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM