gpt4 book ai didi

knockout.js - Knockout 无法处理绑定(bind)

转载 作者:行者123 更新时间:2023-11-28 07:39:49 24 4
gpt4 key购买 nike

当文本未定义时如何绑定(bind)它?例如名称不可用:

<table id="recordTbl" data-bind="visible: records().length &gt; 0" class="table">
<thead>
<tr>
<th class="col-md-4">ID</th>
<th class="col-md-4">Name</th>
</tr>
</thead>
<tbody data-bind="foreach: records">
<tr>
<td data-bind="text: id"></td>
<td data-bind="text: name"></td>
</tr>
</tbody>
</table>

我收到此错误:

Uncaught ReferenceError: Unable to process binding "text: function (){return name }"
Message: name is not defined

最佳答案

你可以$data绑定(bind)上下文属性始终代表当前 View 模型以访问 name通过它:

  <tbody data-bind="foreach: records">
<tr>
<td data-bind="text: id"></td>
<td data-bind="text: $data.name"></td>
</tr>
</tbody>

通过这种方法,如果 records 中的其中一项,KO 不会抛出异常。没有 name属性(property)。

没有$data名为 name 的标识符未定义。然而$data.name始终是一个有效的表达式,它只返回 undefined如果当前 View 模型没有名为 name 的属性.

关于knockout.js - Knockout 无法处理绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201186/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com