【发布时间】:2013-02-06 20:59:16
【问题描述】:
在 Knockout 中的出价方面需要帮助。我完全是淘汰赛的新手。 已尝试通过萤火虫进行故障排除,但没有结果。
这是我的例子
这是淘汰视图模型
function MarketViewModel() {
var self = this;
self.markets = ko.observableArray();
var baseUri = 'api/market';
$.getJSON(baseUri, self.markets);
}
$(document).ready(function () {
ko.applyBindings(new MarketViewModel());
})
顺便说一句,将数组返回为 json 的 API 控制器工作得很好。
我的看法
<!-- Default datatable -->
<div class="block well">
<div class="navbar">
<div class="navbar-inner">
<h5>Default datatable</h5>
</div>
</div>
<div class="table-overflow">
<table class="table table-striped" id="data-table">
<thead>
<tr>
<th>Marknad</th>
</tr>
</thead>
<tbody data-bind="forech: markets">
<tr>
<td data-bind="text: description"></td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- /default datatable -->
@section scripts
{
<script src="~/Scripts/vm/MarketViewModel.js"></script>
}
更新
来自 firebug 的错误:错误:无法解析绑定。 消息:ReferenceError:描述未定义; 绑定值:文本:描述 这将引发无法解析视图中属性“描述”的绑定。 但 JSON 结果包含用于描述的属性。
Json 数组:
[{"$id":"1","MarketId":14,"Description":"SE Projektutveckling","ChUser":"anders.persson","ChTime":"2013-01-28T09:07:50.067"},{"$id":"2","MarketId":39,"Description":"SE Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:01:04.637"},{"$id":"3","MarketId":40,"Description":"NO Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:58.01"},{"$id":"4","MarketId":41,"Description":"GB Styckehus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:50.933"},{"$id":"5","MarketId":42,"Description":"DE Styckehus","ChUser":"anders.persson","ChTime":"2013-02-05T09:39:57.03"},{"$id":"6","MarketId":43,"Description":"SE Fritidshus","ChUser":"anders.persson","ChTime":"2013-01-21T23:00:24.597"}]
【问题讨论】:
-
您能否发布一个示例 json(打印出来的内容是:
$.getJSON(baseUri, function(data) { console.log(data);self.markets(data)});)?你的data-bind="forech: markets"也有错字,应该是data-bind="foreach: markets"。属性的大小写也很重要,确保结果包含属性description而不是Description -
请查看更新后的消息。谢谢!
标签: jquery knockout.js