[英]KnockOut binding View Model
I am to new knockout.js.Very simple.trying to get data(via WebAPI - ajax call). 我要新的基因敲除.js。非常简单。尝试获取数据(通过WebAPI-ajax调用)。 Here is the view
这是视图
<table>
<thead>
<tr>
<td>First Name </td>
<td>Last Name </td>
<td> Email </td>
</tr>
</thead>
<tbody data-bind="foreach:Contact">
<tr>
<td data-bind="text:FirstName"></td>
<td data-bind="text:LastName"></td>
<td data-bind="text:EmailAddress"></td>
</tr>
</tbody>
</table>
Here is my viewmodel and ajax call.Ajax call returns 3 records. 这是我的viewmodel和ajax调用.Ajax调用返回3条记录。 FirstName,LastName and EmailAddress
名字,姓氏和电子邮件地址
<script type="text/javascript" >
$(document).ready(function () {
var data = [];
var viewModel = {
Contact: ko.observableArray(data)
};
$.ajax({
url: "http://localhost/AW/api/Person",
type: "GET",
dataType: "json",
statusCode: {
200: function (contacts) {
viewModel.Contact = contacts;
}
}
});
ko.applyBindings(new viewModel());
});
</script>
As I said API successfully reurning records, But it is not binding .May be I am doing something stupid here. 正如我所说的,API成功地刷新了记录,但是它没有约束力。可能是我在这里做一些愚蠢的事情。
ko.observableArray
(like ko.observable
) is returning a function so you need to set its value with calling it with the new value as the argument: ko.observableArray
(如ko.observable
)正在返回一个函数,因此您需要使用新值作为参数调用它来设置其值:
200: function (contacts) {
viewModel.Contact(contacts);
}
And because your viewModel is an object literal and not a function you need to write: 而且由于您的viewModel是对象文字而不是函数,因此您需要编写:
ko.applyBindings(viewModel);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.