简体   繁体   English

Knockout.js:与索引号为特定Observable Array的模板的绑定

[英]knockoutjs: With-binding to a template with a specific Observable Array object in index number

I am trying to pass a specific object in a ObservableArray defined in my view model to a template with the index number. 我试图将在视图模型中定义的ObservableArray的特定对象传递给具有索引号的模板。

In my view, it looks like this: 在我看来,它看起来像这样:

<!-- ko template: { name: "fooTemplate", with: FooCycles()[0] } --><!-- /ko -->

<script id="fooTemplate" type="text/html">
  //some HTML for a individual FooCycle here
</script>

I got the Uncaught ReferenceError: Unable to process binding "template: function (){return { name:"fooTemplate",with:FooCycles()[0]} }" error. 我收到了Uncaught ReferenceError: Unable to process binding "template: function (){return { name:"fooTemplate",with:FooCycles()[0]} }"错误。 Under the with binding, it still focus the parent VM that it belongs to in my JS debugger(Chrome). 在with绑定下,它仍将焦点集中在我的JS调试器(Chrome)中。

I can get access to a specific array object in my model definition that is used for several ko.computed properties: 我可以在模型定义中访问特定的数组对象,该对象用于几个ko.computed属性:

var fstsum = parseFloat(self.FooCycles()[0].sum());
var sndsum = parseFloat(self.FooCycles()[1].sum());

I can use FooCycles in foreach with no problem: 我可以FooCyclesforeach使用FooCycles

<!-- ko foreach: FooCycles -->
  <div class="item">
    <!-- ko template: { name: "fooTemplate", with: $data } --><!-- /ko -->
  </div>
<!-- /ko -->

FooCycles()[0] works in javascript, but not working in Knockout.js. FooCycles()[0]在javascript中FooCycles()[0] ,但在Knockout.js中不可用。 Is there a way to get an array object with index in Knockout? 有没有办法在淘汰赛中获得带有索引的数组对象?

Template binding does not have with listed under the supported "additional" parameters provided in the documentation. 模板结合不具有with下的文档中提供的支持的“额外”的参数列出。

The reason it works with your foreach is due to: 它与您的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. 如果省略此参数,KO将查找foreach参数,或者将使用当前模型对象作为后备。

Change the with to data and in the case of the foreach you can just omit it. with更改为data ,如果使用foreach ,则可以忽略它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM