![](/img/trans.png)
[英]knockoutjs: binding to an observable property outside of an array, in a template
[英]knockoutjs: With-binding to a template with a specific Observable Array object in index number
我试图将在视图模型中定义的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绑定下,它仍将焦点集中在我的JS调试器(Chrome)中。
我可以在模型定义中访问特定的数组对象,该对象用于几个ko.computed
属性:
var fstsum = parseFloat(self.FooCycles()[0].sum());
var sndsum = parseFloat(self.FooCycles()[1].sum());
我可以FooCycles
在foreach
使用FooCycles
:
<!-- ko foreach: FooCycles -->
<div class="item">
<!-- ko template: { name: "fooTemplate", with: $data } --><!-- /ko -->
</div>
<!-- /ko -->
FooCycles()[0]
在javascript中FooCycles()[0]
,但在Knockout.js中不可用。 有没有办法在淘汰赛中获得带有索引的数组对象?
模板结合不具有with
下的文档中提供的支持的“额外”的参数列出。
它与您的foreach
一起使用的原因是:
数据—一个对象,提供作为要渲染的模板的数据。 如果省略此参数,KO将查找foreach参数,或者将使用当前模型对象作为后备。
将with
更改为data
,如果使用foreach
,则可以忽略它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.