无法使用Knockout将observablecollection绑定到View

时间:2014-05-13 12:31:32

标签: jquery json html5 wcf knockout.js

我的视图模型是:

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"]

1 个答案:

答案 0 :(得分:2)

self.Plines = allData;更改为self.Plines(allData);

另外,改变:

<select data-bind="options: Plines,  optionsText: ''"></select>

<select data-bind="options: Plines,  optionsText: 'line'"></select>

here's a working example