我很困惑,爲什麼我向可觀察數組添加元素時未更新UI。我意識到我沒有對我的AJAX getJSON調用數據做任何事情,但是當我向它添加元素時不應該更新我的可觀察數組?它顯示「test1」正常,但不顯示「test2」。沒有PHP或JS錯誤,它只是不更新UI。使用jQuery getJSON敲除JS
HTML:
<div class="allScheduleWrap" data-bind="foreach: schedules">
<div class="schedule">
<div class="downTime" data-bind="text:downTime"></div>
</div>
</div>
JS:
$(document).ready(function() {
var AllSchedules = [];
AllSchedules.push({downTime: "test1"});
function SchedulesViewModel() {
var self = this;
self.schedules = ko.observableArray(AllSchedules);
}
ko.applyBindings(new SchedulesViewModel());
$.getJSON("GetSchedules.php", function(data) {
AllSchedules.push({downTime: "test2"}); //does NOT update the UI
});
});
謝謝!我覺得很愚蠢。嘿 – Buchannon