我在 KnockoutJS 中使用样式绑定时遇到困难。
<script id="avatarTemplate" type="text/x-jquery-tmpl">
<div id="avatar_${id}" class="avatar" data-bind="style:
{ background: s, width: '50px', height: '85px', left: (x + 'px'), top:
(y + 'px') }">${s}, ${x}, ${y}</div>
</script>
<div data-bind="template: { name: 'avatarTemplate', foreach: avatars }"></div>
渲染该模板的结果是:
<div id="avatar_1" class="avatar" style="width: 50px; height: 85px;">avatar.png, 0, 0</div>
谁能帮我弄清楚为什么所有依赖于视图模型的样式都不显示?
If x
and y
是可观察的,那么你需要这样指定它:
<div id="avatar_${id}" class="avatar" data-bind="style:
{ background: s, width: '50px', height: '85px', left: (x() + 'px'), top:
(y() + 'px') }">${s}, ${x}, ${y}</div>
如果在表达式中使用可观察量,则需要用 () 指定它,因为它不会自动解包。
http://jsfiddle.net/rniemeyer/6GtV3/ http://jsfiddle.net/rniemeyer/6GtV3/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)