[英]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.