之間如何相互參照的對象我有2種型號:Knockout.js - 兩個數組
var ProductViewModel = function() {
var self = this;
self.ProductList = ko.observableArray([]);
self.ProductTypeList = ko.observableArray([]);
//...init 2 array and some misc methods
}
:
var ProductModel = function(productid, productname, producttypeid, productprice) {
var self = this;
self.ProductId = productid;
self.ProductName = productname;
self.ProductTypeId = producttypeid;
self.ProductPrice = productprice;
}
和
var ProductTypeModel= function(producttypeid, producttypename) {
var self = this;
self.ProductTypeId = producttypeid;
self.ProductTypeName = producttypename;
}
我在一個視圖模型中使用它們在html文件中:
...
<table>
<thead>
<tr>
<th>Id</th>
<th>Type</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody data-bind="template: { name: 'row', foreach: ProductList }">
</tbody>
</table>
...
<script id="row" type="html/template">
<tr>
<td data-bind="html: ProductId"></td>
<td data-bind="html: ProductName"></td>
<td data-bind="html: ProductTypeId"></td> <!-- I stuck here!!! -->
<td data-bind="html: ProductPrice"></td>
</tr>
</script>
...
我希望我的表顯示ProductTypeName而不是ProductTypeId,但我無法將ProductTypeId傳遞到任何函數中以獲取ProductTypeName。
你的問題是不明確的有3類,並沒有:)你shuld定義類型和其他類型的使用他們的任何模型或控制器在完成這些步驟之後,您可以使用foreach作爲類型實例 – dewelloper
@HamitYıldırım,在您的html中調用它們,我認爲它很清楚。我可以綁定我的viewmodel並正確使用它。 'ProductViewModel'可能有點類似於你所說的控制器。 –