我在这里用头撞墙,试图弄清楚为什么我不能让它工作。我已经尝试了许多示例,并将我尝试过的所有内容都简化为下面的内容。
所以,在我的 aspx 页面上,我有:
<input type="radio" data-bind="value: individual" />Individual
这是我的javascript:
var serviceBase = 'http://localhost:49906/PopulationSelection.aspx/';
var getSvcUrl = function (method) { return serviceBase + method; };
var ajaxGetJson = function (method, jsonIn, callback) {
$.ajax({
url: getSvcUrl(method),
type: "GET",
data: ko.toJSON(jsonIn),
dataType: "json",
contentType: "application/json",
success: function (json) {
callback(json.d);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr + ' ' + thrownError);
}
});
}
var batchesDataService = {
getSavedBatches: function (callback) {
ajaxGetJson("GetBatches", null, callback);
}
};
var Batch = function (p) {
this.individual = ko.observable(p.Individual);
this.household = ko.observable(p.Household);
this.countOnly = ko.observable(p.CountOnly);
this.femalePrimary = ko.observable(p.FemalePrimary);
this.eventManagement = ko.observable(p.EventManagement);
this.eventManagementText = ko.observable(p.EventManagementText);
this.randomSampling = ko.observable(p.RandomSampling);
this.randomSamplingText = ko.observable(p.RandomSamplingText);
this.stateHasChanged = ko.observable(false);
};
var loadBatchesCallback = function (data) {
var parsed = JSON.parse(data);
myViewModel.Batch = new Batch(parsed);
//also tried:
//myViewModel.Batch(new Batch(parsed));
};
var myViewModel;
var viewModel = function () {
this.Batch = ko.observable();
this.getBatchInfo = function () {
batchesDataService.getSavedBatches(loadBatchesCallback);
};
};
$(document).ready(function () {
myViewModel = new viewModel();
myViewModel.getBatchInfo();
ko.applyBindings(myViewModel.Batch);
});
我从我的网络方法(用于会话访问)中获取数据没有问题,当我提醒批处理成员时,我看到了正确的信息。
我的问题来自实际的ko.applyBindings()
. 无论我尝试了什么,我都会在控制台中收到以下错误:
Uncaught Error: Unable to parse bindings.
Message: ReferenceError: individual is not defined;
Bindings value: value: individual
任何帮助表示赞赏!