$parent.inState 不是 Knockout JS 的函数错误

发布于 2024-12-28 08:45:55 字数 1005 浏览 0 评论 0原文

我正在使用命名模板并将数据列表绑定到该模板。绑定效果很好,但我在 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-01-04 08:45:55

当您将 data 作为人员传递时,您需要对 $data 进行 foreach (除非您的结构确实是 people.people 。

您可以这样做:

<div>
  <ul data-bind="template: { name: 'peopleScript', foreach: people }"></ul>
</div>

只需在模板中或使用当前结构中的 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 to foreach on $data (unless your structure is really people.people.

You could do:

<div>
  <ul data-bind="template: { name: 'peopleScript', foreach: people }"></ul>
</div>

and just have the li in the template or with your current structure, do your foreach on $data.

The issue with inState is that the template creates one scope, then the foreach 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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文