이 질문을 ko.observableArray()
var SimpleListModel = function(items) {
this.items = ko.observableArray(items);
this.itemToAdd = ko.observable("");
this.addItem = function() {
if (this.itemToAdd() != "") {
this.items.push(this.itemToAdd()); // Adds the item. Writing to the "items" observableArray causes any associated UI to update.
this.itemToAdd(""); // Clears the text box, because it's bound to the "itemToAdd" observable
}
}.bind(this); // Ensure that "this" is always this view model
};
ko.applyBindings(new SimpleListModel(["Alpha", "Beta", "Gamma"]));
사용하여 작업을 관찰 할 수있는 배열에 정상적인 배열을 변환 할 수 있습니다 나에게 의미가 있지만 여기에 -
배열을 관찰 가능한 배열로 변환하려면 다음을 수행하십시오.
var normalArray = ['1', '2']
normalArray = ko.observableArray(normalArray);
// normalArray is now an observableArray
참고로 ko.utils.arrayForEach에는 관찰 가능한 배열이 필요하지 않습니다. 원하는 경우 $. 대신에 각각을 말할 수도 있습니다. –