모델 클래스와 뷰 모델이 있는데 모델 클래스의 인스턴스를 만들고 있습니다.텍스트 상자 값을 viewmodel에 넘기는 jock
입력 태그 안에 텍스트를 입력하면 값이 뷰 모델에 들어 가지 않습니다. "저장"버튼을 클릭하면 빈 배열이 나타납니다.
실제로이 위치 한 반면, 당신은, 중첩 된 curretUser
뷰 - 모델에 saveuserDetail
함수에 <input type="button"/>
요소에 click
도 구속했습니다
// Model class///
var userModel = function() {
self = this;
self.userName = ko.observable()
self.userMobileNumber = ko.observable();
self.userEmail = ko.observable();
self.userImageBase64 = ko.observable();
self.userImageType = ko.observable();
self.deviceUId = ko.observable();
}
//ViewModel//
var userDetails = function() {
self = this;
self.currentUser = ko.observable(new userModel());
//ajax post
self.saveuserDetail = function() {
var model = ko.toJSON(currentUser());
console.log(model);
jQuery.support.cors = true;
$.ajax({
url: baseurl + 'api/xxxx/xxxxx',
type: 'POST',
data: model,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function(data) {}
});
}
};
$(document).ready(function() {
ko.applyBindings(new userDetails());
});
'self'는'var' :'var self = this;'로 로컬해야합니다. 'var model = ko.toJSON (self.curretUser());에서'self'가 필요합니다. ' –
덕분에 제 문제가 해결되었습니다. –