如何使用$ data对象使用foreach绑定(knockout.js) [英] How to use foreach binding (knockout.js) with the $data object
问题描述
对我来说,奇怪的是
< div data-bind =template:{name:'input-template',foreach:$ root.geometries ['Kubus']。invoer}>< / div>
有效,而$ data是'Kubus'
但那
< div data-bind =template:{name:'input-template',foreach :$ root.geometries [$ data] .invoer}>< / div>
给出了一个javascript错误信息:
无法解析绑定。消息:TypeError:$ root.geometries [$ data]
未定义;绑定值:template:{name:'input-template',
foreach:$ root.geometries [$ data] .invoer}
- 你能解释为什么会发生这种情况吗?
- 你能告诉我应该怎么做,所以错误不会显示吗?
- 有什么方法可以做我想用更高效的代码做的事情吗?
对于我来说,对于:dimSelect
和使用:geoSelect
来说太复杂了。为什么不直接使用 dimSelect()
和 geoSelect()
?
http://jsfiddle.net/jearles/PYbzF/12/ p>
for some reason I cannot get a foreach binding to work with a $data object in it, I have posted the code on JSfiddle
the weird thing for me is that
<div data-bind="template: { name: 'input-template', foreach: $root.geometries['Kubus'].invoer }"></div>
works, and that $data is 'Kubus'
but that
<div data-bind="template: { name: 'input-template', foreach: $root.geometries[$data].invoer }"></div>
gives a javascript error message:
Unable to parse bindings. Message: TypeError: $root.geometries[$data] is undefined; Bindings value: template: { name: 'input-template', foreach: $root.geometries[$data].invoer }
- can you explain why this happens?
- can you tell me how I should do it so the error doesn't show?
- are there ways to do what I'm trying to do with more efficient code?
To me you are being too tricky with the with: dimSelect
and with: geoSelect
. Why not just use dimSelect()
and geoSelect()
directly?
http://jsfiddle.net/jearles/PYbzF/12/
这篇关于如何使用$ data对象使用foreach绑定(knockout.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!