css - 有条件地改变angularjs元素的颜色?

标签 css angularjs conditional

我对 AngularJS 还是很陌生。我正在尝试更改表格元素的颜色,以便在用户对此选项进行投票时将其变为黄色。

<div ng-show="poll.userVoted">
    <table class="result-table">
        <tr ng-repeat="choice in poll.choices">
            <td>{{choice.text}}</td>
            <td>
            <table ng-if="choice.text == poll.userChoice.text" style="background-color: yellow; width: {{choice.votes.length/poll.totalVotes*100}}%; text-align: right">
                <tr>

                        <td>{{choice.votes.length}}</td>
                </tr>
            </table>
            <table ng-if="choice.text != poll.userChoice.text" style="background-color: lightblue; width: {{choice.votes.length/poll.totalVotes*100}}%; text-align: right">
                <tr>

                    <td>{{choice.votes.length}}</td>
                </tr>
            </table>
            </td>
        </tr>
    </table>

enter image description here

最佳答案

这是通过使用 ng-class 完成的。

像这样在你的 td 上使用 ng-class:

<td ng-class="{yellowstyle: choice.text==poll.userChoice.text}">...</td>

当您的条件为真时,会将 css 类黄色样式放在元素上。

在你的例子中:

<table class="result-table">
  <tr ng-repeat="choice in poll.choices">
    <td>{{choice.text}}</td>
    <td>
      <table style="width: {{choice.votes.length/poll.totalVotes*100}}%; text-align: right">
        <tr>
          <td ng-class="{yellowstyle: choice.text==poll.userChoice.text, lightbluestyle: choice.text!=poll.userChoice.text}">{{choice.votes.length}}</td>
        </tr>
      </table>
    </td>
  </tr>
</table>

使用 style.css 文件:

.yellowstyle {background-color: yellow;}
.lightbluestyle {background-color: lightblue;}

关于css - 有条件地改变angularjs元素的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26093075/

相关文章:

html - 右侧相对于视口(viewport)的 CSS 相对位置

json - AngularJS 最可靠的 JSON 查看器/编辑器模块

ms-word - Word 2010条件 block

javascript - 我如何检查是否在 JS 中单击了多个 div?

css 边距不适用于 jsf

angularjs - 使用纯 angularjs 实现自动完成,无需任何依赖

angularjs - 在嵌套模型上使用填充从 Angular.js 查询到 Sails.js 和水线

nil 类型的 Lua 条件

python - 哪个 Python 条件返回语句最 Pythonic?

css - 我怎样才能让这些社交分享者的风格正确?