我的视图模型是:
function MyInfo(data) {
this.line = ko.observable(data);
}
function MyViewModel() {
var self = this;
self.Plines = ko.observableArray([]);
$.getJSON("URL",
function (allData) {
var mappedData = $.map(allData, function (item) { return new MyInfo(item) });
self.Plines = mappedData;
});
alert(self.Plines);
}
ko.applyBindings(new MyViewModel());
View只有Select。
<select data-bind="options: Plines, optionsText: ''"></select>
我的JSON响应是:
["Item1","Item2","Item3","Item4","Item5"]
答案 0 :(得分:2)
将self.Plines = allData;
更改为self.Plines(allData);
另外,改变:
<select data-bind="options: Plines, optionsText: ''"></select>
到
<select data-bind="options: Plines, optionsText: 'line'"></select>