Knockout.js-了解foreach和 [英] Knockout.js -- understanding foreach and with

查看:91
本文介绍了Knockout.js-了解foreach和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在学习learning.knockout.js教程并进行实验.有人可以解释为什么它有效吗[教程:单页应用程序,步骤2 ](使用with: chosenFolderDataforeach: mails):

I've been going through the learn.knockout.js tutorials and been experimenting. Can someone explain why this works [Tutorial: Single page applications, Step 2] (using with: chosenFolderData and foreach: mails):

<!-- Mails grid -->
<table class="mails" data-bind="with: chosenFolderData">
    <thead><tr><th>From</th><th>To</th><th>Subject</th><th>Date</th></tr></thead>
    <tbody data-bind="foreach: mails">
        <tr>
            <td data-bind="text: from"></td>
            <td data-bind="text: to"></td>
            <td data-bind="text: subject"></td>
            <td data-bind="text: date"></td>
        </tr>     
    </tbody>
</table>

但不行(仅使用foreach: chosenFolderData.mails):

<!-- Mails grid -->
<table class="mails">
    <thead><tr><th>From</th><th>To</th><th>Subject</th><th>Date</th></tr></thead>
    <tbody data-bind="foreach: chosenFolderData.mails">
        <tr>
            <td data-bind="text: from"></td>
            <td data-bind="text: to"></td>
            <td data-bind="text: subject"></td>
            <td data-bind="text: date"></td>
        </tr>     
    </tbody>
</table>

怀疑是因为chosenFolderData是可观察的,而chosenFolderData.mails不是.有人可以告诉我吗?

I suspect it's because while chosenFolderData is observable, chosenFolderData.mails is not. Can anyone tell me for certain?

非常感谢!

-拉尔夫

推荐答案

因为您实际上并未以编写方式访问所需的属性.在模型中,chosenFolderData是可观察的,必须像调用方法一样调用以获取值.要在不使用with的情况下提供功能(并且我建议在由于开销而需要高性能的情况下,请不要使用with)...

Because you are not actually accessing the property you want with the way it is written. In the model chosenFolderData is an observable and must be called like a method to retrieve the value. To provide the functionality without using with (and I suggest not using with where high performance is necessary because of the overhead)...

<tbody data-bind="foreach: chosenFolderData().mails">

这篇关于Knockout.js-了解foreach和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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