【问题标题】:Conditionally change CSS with AngularJS使用 AngularJS 有条件地更改 CSS
【发布时间】:2013-09-16 01:00:28
【问题描述】:

我正在尝试根据 Angular 表达式的值更改 div 的背景颜色。我有以下 div:

<div class="outer">
  <h4 class="weather-heading">It's currently {{weather}}</h4>
</div>

基于 {{weather}} 的值(即晴天、阴天),我想更改外部 div 的背景颜色。使用 Angular 执行此操作的最佳方法是什么?

【问题讨论】:

  • 你看过ngClass吗?
  • 最好的办法是将视图的逻辑放在指令中
  • @Cherniv 你能举个代码例子吗?
  • 你可以按照 incutonez 的建议从 ngClass 开始

标签: css angularjs angular-ui


【解决方案1】:

正如@incutonez 所说,您可以使用ng-class

<div ng-class="{'sunny': weather == 'Sunny', 'rainy': weather == 'Rainy'}">
  <h4 class="weather-heading">It's currently {{weather}}</h4>
</div>

Plunkerhere.

根据您要完成的任务,创建指令可能更合适,正如@Cherniv 所建议的那样。

【讨论】:

    猜你喜欢
    • 2018-08-29
    • 2013-07-31
    • 2021-10-01
    • 2019-11-11
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2012-11-28
    • 2017-05-07
    相关资源
    最近更新 更多