2014-09-29 73 views
2

我还是AngularJS的新手。我试图改变表格元素的颜色,使其黄色,如果用户投票选择。有条件地改变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

+0

什么错误? – hfarazm 2014-09-29 05:39:52

+1

你可以尝试这样的事情,如果我正确理解你的问题标题 - http://plnkr.co/edit/jeszGorMQmLiqhtWDt94?p=preview – swapnesh 2014-09-29 05:44:57

+0

谢谢我将prob做这样的事情。我只是觉得我可以在某种程度上做到这一点,而不用为它制作功能。 – jsky 2014-09-29 06:11:22

回答

7

这是通过使用纳克级完成。

使用纳克级上的TD是这样的:

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

,将放在CSS类yellowstyle的项目时你的病情是真实的。

而且在你的榜样:

<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;} 
+0

这给了我一个语法错误: '错误:语法错误:令牌':'是表达式[12]处的意外令牌[yellowstyle:choice.text == poll.userChoice.text,lightbluestyle:choice.text!= poll.userChoice.text]开始于[:choice.text == poll.userChoice.text,lightbluestyle:choice.text!= poll.userChoice.text] .' – jsky 2014-09-29 06:04:16

+1

对不起,忘记了{} in ng-class,fixed,try再次 – 2014-09-29 06:21:59

+0

presto!谢谢你,先生。 – jsky 2014-09-29 06:24:42