【发布时间】:2016-12-08 18:38:27
【问题描述】:
我是 Knockout JS 的新手,我正在尝试将 ajax 结果数据绑定到 Knockout JS 视图模型,但是在将数据绑定到视图时遇到了问题,我已经创建了模型和视图模型,并且我从阿贾克斯。需要帮助。
下面是我的代码:
// ajax on page load///
$.ajax({
type: "POST",
dataType: "json",
url: baseUrl + 'api/xxx/xxx',
data: UserProfileModel,
success: function(data) {
result = data;
////view model////
userDetailsViewModel(result);
},
error: function(error) {
jsonValue = jQuery.parseJSON(error.responseText);
//jError('An error has occurred while saving the new part source: ' + jsonValue, { TimeShown: 3000 });
}
});
//// view model///
var userDetailsViewModel = function(result) {
console.log(result);
self = this;
self.user = ko.observable(new userModel(result));
};
$(document).ready(function() {
ko.applyBindings(userDetailsViewModel());
});
/// Model////
function userModel(result) {
this.name = ko.observable();
this.userName = ko.observable();
}
【问题讨论】:
-
userDetailsViewModel 应该返回自我。目前它没有返回任何东西。因此 View 没有 ViewModel 公开的属性。因此,您的代码不起作用。
标签: javascript jquery ajax knockout.js