$parent.inState 不是 Knockout JS 的函数错误
我正在使用命名模板并将数据列表绑定到该模板。绑定效果很好,但我在 $parent.inState() 调用时收到错误。看下面的示例:
<div data-bind="template: { name: 'peopleScript', data: people }"> </div>
<script id="peopleScript" type="text/html">
<ul data-bind="foreach: people">
<li>
Name: <span data-bind="text: name"> </span>
State: <span data-bind="{ text: state, css: { outOfState: !$parent.inState($data) } }"> </span>
<span data-bind="visible: ($parent.inState($data))">
In State
<span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>
</script>
它说 $parent.inState
不是一个函数。我也尝试过 $parents
,但没有成功。我知道应用绑定的代码有效;如果我只是内联模板,效果就很好。我也知道其他一切都设置正常,并且它可以很好地访问该方法。所以只是由于某种原因找不到 inState 。
有什么想法吗?
谢谢。
I'm using a named template and binding a list of data to that. The binding works great, but I get the error on $parent.inState() call. Looking at the sample below:
<div data-bind="template: { name: 'peopleScript', data: people }"> </div>
<script id="peopleScript" type="text/html">
<ul data-bind="foreach: people">
<li>
Name: <span data-bind="text: name"> </span>
State: <span data-bind="{ text: state, css: { outOfState: !$parent.inState($data) } }"> </span>
<span data-bind="visible: ($parent.inState($data))">
In State
<span>
<a href="#" data-bind="click: $parent.removePerson">Remove</a>
</li>
</ul>
<button data-bind="click: addPerson">Add</button>
</script>
it says $parent.inState
is not a function. I already tried $parents
too, but to no avail. I know the code to apply the bindings works; if I just have the template inline, it works great. I also know everything else is setup OK and that it can access the method just fine. So it's simply it can't find inState for some reason.
Any ideas why?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您将
data
作为人员传递时,您需要对$data
进行foreach
(除非您的结构确实是people.people 。
您可以这样做:
只需在模板中或使用当前结构中的
li
,即可在$data
上执行foreach
.inState
的问题在于template
创建一个作用域,然后foreach
绑定创建另一个作用域,因此,当您进入其中时,您必须使用来执行此操作。 >$parents[1]
例如:http://jsfiddle.net/rniemeyer/RNWML/ 或者如果真的位于顶层,那么使用$root
是最简单的选择,例如: http ://jsfiddle.net/rniemeyer/RNWML/2/When you are passing
data
as people, then you would want toforeach
on$data
(unless your structure is reallypeople.people
.You could do:
and just have the
li
in the template or with your current structure, do yourforeach
on$data
.The issue with
inState
is that thetemplate
creates one scope, then theforeach
binding creates another. So, when you are inside, you would have to go up two scopes. You could do this by using$parents[1]
like: http://jsfiddle.net/rniemeyer/RNWML/ or if it really is at the top level, then using$root
is the easiest choice like: http://jsfiddle.net/rniemeyer/RNWML/2/