css - 父级应该如何为 Angular 的子组件设置样式?我想要一个不会被弃用的好解决方案

标签 css angular components parent-child transclusion

当我需要为新用途稍微更改组件时,我查看了嵌入和多槽嵌入与在组件周围添加 HTML 的对比,但仍然存在组件的内部样式需要更改的情况。

最佳答案

您可以将样式作为输入传入,然后使用 [ngStyle] 在模板中进行设置。

因此,对于一个简单的例子,我将展示如何使用背景颜色来做到这一点。

自定义组件.html

<div [ngStyle]="{'background-color': data?.backgroundColor }">
   <p> the background color will be set from an input </p>
</div>

自定义组件.ts

  @Input('data') 
  data: {
    backgroundColor: string;
  };

parent.html

<customComponent [data]="{backgroundColor: 'grey'}"></customComponent>

关于css - 父级应该如何为 Angular 的子组件设置样式?我想要一个不会被弃用的好解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51768801/

相关文章:

Javascript:移动浏览器的 "view point"

css - Angular 2 中的 ngClass 不在表格中呈现

symfony - 使用 symfony2 路由组件(在 symfony2 之外)

javascript - Mounted in child component executes before parent component created 问题

javascript - ngFor 在 Angular2 中更新依赖变量后不会触发

html - Firefox 破坏 Bootstrap 行显示

html - CSS 显示 : inline-block drops to next line

javascript - Vue Router 从子节点链接子节点

angular - 如何获取带有参数的 Url 路径以匹配它以检查 Angular4+ 中的访问?

java - 如何解决 alfresco sdk 中的 alfresco webscripts 中的 CORS 错误?