Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Knockout.js 如何使用样式数据绑定?_Knockout.js - Fatal编程技术网

Knockout.js 如何使用样式数据绑定?

Knockout.js 如何使用样式数据绑定?,knockout.js,Knockout.js,我很难在KnockoutJS中使用样式绑定 ${s},${x},${y} 呈现此模板的结果是: avatar.png,0,0 有人能帮我弄清楚为什么依赖于视图模型的所有样式都不显示吗 如果x和y是可观察的,则需要如下指定: <div id="avatar_${id}" class="avatar" data-bind="style: { background: s, width: '50px', height: '85px', left: (x() + 'px'), top

我很难在KnockoutJS中使用样式绑定


${s},${x},${y}
呈现此模板的结果是:

avatar.png,0,0

有人能帮我弄清楚为什么依赖于视图模型的所有样式都不显示吗

如果
x
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> 
<div data-bind="style: { backgroundImage: src }">
${s},${x},${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> 
<div data-bind="style: { backgroundImage: src }">
我发现,如果
src
值不是有效的背景图像属性,它将被完全忽略

我必须使用:

<div data-bind="style: { backgroundImage: 'url(\'' + src() + '\'' }">


可能会在某个时候为某人省去一些痛苦:)

Ahh,我想知道如何进行KnockoutJS连接。。谢谢你:哦,我的上帝!只是花了30分钟想弄明白=谢谢你。节省我的时间。我正确地访问了x()和y(),但我没有想到我需要添加单位“px”。谢谢你抽出时间!这正是我正在寻找的用例!