问题
I have a viewmodel like
AppViewModel = {
agent : ko.observableArray([ {
name : 'test',
age : '23'
}])
};
My json data comes like
{"agent":[{"name":"john","age":"23"},{"name":"conor","age":"23"}]}
for ajaxcall evry 3 sec
How to replace the view model with new data
I tried
success : function(responseData) {
var data = ko.toJS(responseData);
//AppViewModel.agent.push(data);
AppViewModel.agent.replace(agent,data);
}
but doest work.
回答1:
All you have to do is set the observable
success : function(responseData) {
var data = ko.toJS(responseData);
AppViewModel.agent(data.agent);
}
回答2:
You can just assign new data to array:
success : function(responseData) {
var data = ko.toJS(responseData);
AppViewModel.agent(data);
}
来源:https://stackoverflow.com/questions/20091586/replace-observablearray-with-new-data