我正在学习 Knockout,我想对不同的 url 进行多个服务器调用。我对淘汰赛了解不多,但我知道你不能,也不应该多次使用 applyBinding,所以我有以下内容:
<h2 data-bind="text: data2().user, valueUpdate: 'afterkeydown'">User</h2>
<h2 data-bind="text: data2().id">ID</h2>
<input type="text" data-bind="value: user, valueUpdate: 'afterkeydown'" placeholder="text goes here" />
<div data-bind="foreach: data1()">
<div data-bind="text: name"></div>
<div data-bind="text: count"></div>
</div>
<script src="~/Scripts/knockout-3.1.0.js"></script>
<script>
var viewModel = function () {
var self = this;
self.data1 = $.getJSON('http://localhost:57635/api/Stats/GetAllStats', function (data) {
name: data.Name;
count: data.Count
});
self.data2 = $.getJSON('http://localhost:57635/api/Stats/GetItem', function (data) {
user: ko.observable(data.ItemName);
id: data.Id
});
};
ko.applyBindings(viewModel)
</script>
如果有人可以朝着正确的方向迈出一步或进行修复,将不胜感激。
我收到的控制台中的错误如下:
Uncaught TypeError: Unable to process binding "text: function (){return data2().user }"
Message: object is not a function
亲切的问候
詹姆士