【发布时间】:2019-11-03 03:12:02
【问题描述】:
我正在尝试将样式应用于自定义组件的子组件。
选择器:
<custom-component-example></custom-component-example>
custom-component-example.html 内部:
<button>
<ng-content></ng-content>
</button>
如果我要使用这样的风格:
<custom-component-example style="color: green">some text</custom-component-example>
或者像这样:
<custom-component-example [ngStyle]="{'color': green}">some text</custom-component-example>
按钮文本不会变绿。样式可以是任何东西(例如字体粗细或大小或任何东西)。
我也试过这个话题的解决方案:
Best way to pass styling to a component
但这也不适用于子元素(上例中的按钮)。
如何传递任何给定的样式并将其应用于子元素,在示例的情况下,我将如何传递样式(通过 custom-component-example 选择器)并将其应用于按钮和按钮的文本?
提前致谢。
【问题讨论】:
-
由于它是 Angular 2+,每个自定义组件都应该在定义时分配一个 CSS 文件或内联样式。这些样式可能是静态的。为什么我们要在运行时注入它?
-
我猜,Angular 2+ 中的每个组件都应该是独立的。
-
我认为这两种方法都没有问题,您能否检查一下,从样式到按钮的位置,样式可能存在重量/优先级问题。当您编写样式属性时,它独立于 angular/javascript。它只是 HTML 和 CSS
标签: javascript html css angular