简体   繁体   中英

How do I replace the contents of knockout js observable array with an array returned from a promise

I am trying to replace the contents of an observable array with an array that is returned from a promise.

I initialize an observable array like so

let contents = ko.observableArray([]);

And the array that is returned from the promise looks something like this

[{name : test, code : 0, country : UK}]

How can replace the empty observable array with the contents of the array that is returned from the promise?

I have tried this

promise.then(array => {contents(array)});

As well as this

promise.then(function(value){contents(value)})

but neither of them add anything into the observable array.

Check this link

you should first delete its content (if any) through observableArray method called removeAll.

// empty the array
contents.removeAll()

// insert the new array values
promise.then(array => {ko.utils.arrayPushAll(contents, array)});

// or
promise.then(function(value){ko.utils.arrayPushAll(contents, value)});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM