【发布时间】:2015-04-21 14:17:37
【问题描述】:
我有 2 个模型:
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;
}
我在视图模型中使用它们:
var ProductViewModel = function() {
var self = this;
self.ProductList = ko.observableArray([]);
self.ProductTypeList = ko.observableArray([]);
//...init 2 array and some misc methods
}
在 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 种类型并且没有任何模型或控制器 :) 在您可以在 html 中使用 foreach 调用它们的步骤之后,您应该定义类型并在另一种类型中使用它们作为类型实例
-
@HamitYıldırım,我想得很清楚。我可以绑定我的视图模型并正确使用它。
ProductViewModel可能有点像你所说的控制器。
标签: knockout.js