我试图将 View 模型中定义的 ObservableArray
中的特定对象传递给具有索引号的模板。
在我看来,它看起来像这样:
<!-- ko template: { name: "fooTemplate", with: FooCycles()[0] } --><!-- /ko -->
<script id="fooTemplate" type="text/html">
//some HTML for a individual FooCycle here
</script>
我收到 Uncaught ReferenceError: Unable to process binding "template: function (){return { name:"fooTemplate",with:FooCycles()[0]} }"
错误。在 with 绑定(bind)下,它仍然在我的 JS 调试器(Chrome)中关注它所属的父虚拟机。
我可以访问模型定义中的特定数组对象,该对象用于多个 ko.compulated
属性:
var fstsum = parseFloat(self.FooCycles()[0].sum());
var sndsum = parseFloat(self.FooCycles()[1].sum());
我可以在 foreach
中使用 FooCycles
,没有任何问题:
<!-- ko foreach: FooCycles -->
<div class="item">
<!-- ko template: { name: "fooTemplate", with: $data } --><!-- /ko -->
</div>
<!-- /ko -->
FooCycles()[0]
在 JavaScript 中工作,但在 Knockout.js 中不起作用。有没有办法在 Knockout 中获取带有索引的数组对象?
最佳答案
Template binding文档中提供的受支持的“附加”参数下没有列出 with
。
它与您的 foreach
配合使用的原因是:
data — an object to supply as the data for the template to render. If you omit this parameter, KO will look for a foreach parameter, or will fall back on using your current model object.
将 with
更改为 data
,对于 foreach
,您可以省略它。
关于javascript - knockout : With-binding to a template with a specific Observable Array object in index number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24072920/