【问题标题】:Angular 2 - conditional styling for descendantsAngular 2 - 后代的条件样式
【发布时间】:2017-08-01 07:22:18
【问题描述】:

.parent {
  color: blue;
}

.parent * {
  color: red;
}

.child {
  color: black;
}

.grandchild {
  color: green;
}
<div class="parent">
  Parent
  <div>Child
    <div>GrandChild</div>
  </div>
</div>

假设我在 Angular 组件中有一个名为 toggle 的属性。我想为后代切换“.parent *”样式,以便它们的颜色会根据切换状态在红色或蓝色之间变化。我怎样才能通过 ngClass 或 ngStyle 做到这一点?或者有可能吗?谢谢!

【问题讨论】:

    标签: css angular ng-class ng-style


    【解决方案1】:

    只需使用[class.parent]即可

    <div [class.parent]="toggle">
     Parent
     <div>Child
      <div>GrandChild</div>
     </div>
    </div>
    

    toggle 为真时,将应用类父级。

    【讨论】:

    • 巴巴尔,这就像一个魅力!我不明白的是 class.parent 是如何从 .parent * 而不是 .parent 获取课程的。角度文档没有解释:angular.io/docs/ts/latest/guide/cheatsheet.html.
    • 尝试颠倒它们在 css 文件中的顺序。最后一个定义在 CSS 中以相同的特异性获胜。
    猜你喜欢
    • 2017-05-22
    • 1970-01-01
    • 2016-10-02
    • 2018-09-23
    • 2017-07-18
    • 2016-07-15
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    相关资源
    最近更新 更多