如何使用$ data对象使用foreach绑定(knockout.js) [英] How to use foreach binding (knockout.js) with the $data object

查看:208
本文介绍了如何使用$ data对象使用foreach绑定(knockout.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我不能得到一个foreach绑定与$ data数据对象的工作,我已经把代码贴在 JSfiddle



对我来说,奇怪的是

 < 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}




  1. 你能解释为什么会发生这种情况吗?

  2. 你能告诉我应该怎么做,所以错误不会显示吗?
  3. 有什么方法可以做我想用更高效的代码做的事情吗?

对于我来说,对于: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 }

  1. can you explain why this happens?
  2. can you tell me how I should do it so the error doesn't show?
  3. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆