[英]Data-bind style not working with if else condition
I'm trying to change the background of a div depending from the value I'm getting from the js file. 我试图根据我从js文件中获取的值来更改div的背景。
So if the value is different from 0 I would like to have one background and if its 0 I want another background. 因此,如果该值不同于0,我想有一个背景,如果它的值为0,我想有另一个背景。
This is the markup I'm trying with: 这是我正在尝试的标记:
<tbody data-bind="foreach: skills">
<tr>
<td>
<div data-bind="attr: { id: Id }, style: { background: Outstanding != '0' ? 'none repeat scroll 0 0 #B33A3A;' : 'none repeat scroll 0 0 #396eac;' }">
<span data-bind="text: Name" style="color: #ffffff;"></span>
</div>
<div>
<span data-bind="visible: Outstanding != '0', text: Outstanding + ' Needed'" style="color: #365474"></span><br/>
<span data-bind="text: Employees" style="color: #365474"></span>
</div>
</td>
</tr>
</tbody>
Do you guys see anything not correct here? 你们看到这里有什么不正确的地方吗?
PS. PS。 Using the Chrome addon Knockoutjs context debugger I was able to see that the actual value of
Outstanding
is 0: 使用Chrome插件Knockoutjs上下文调试器,我可以看到
Outstanding
的实际值为0:
You have unnecessary semicolon (;) at the end of the background value. 您在背景值的末尾有不必要的分号(;)。 Change the line to be:
将行更改为:
<div data-bind="attr: { id: Id }, style: { background: Outstanding != '0' ? 'none repeat scroll 0 0 #B33A3A' : 'none repeat scroll 0 0 #396eac' }">
That should fix it. 那应该解决它。
See example below: 请参见下面的示例:
(function() { 'use strict'; function MyVM() { var self = this; self.skills = ko.observableArray([ { Id: 1, Outstanding: '0', Name: 'Foo', Employees: 'Person A' }, { Id: 2, Outstanding: '1', Name: 'Bar', Employees: 'Person B' } ]); } ko.applyBindings(new MyVM(), document.body); }());
<script src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.2.0/knockout-min.js"></script> <table> <tbody data-bind="foreach: skills"> <tr> <td> <div data-bind="attr: { id: Id }, style: { background : Outstanding !== '0' ? 'none repeat scroll 0 0 #B33A3A' : 'none repeat scroll 0 0 #396eac' }"> <span data-bind="text: Name" style="color: #ffffff;"></span> </div> <div> <span data-bind="visible: Outstanding != '0', text: Outstanding + ' Needed'" style="color: #365474"></span><br/> <span data-bind="text: Employees" style="color: #365474"></span> </div> </td> </tr> </tbody> </table>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.