Replacing item in observableArray

前端 未结 3 1299
半阙折子戏
半阙折子戏 2021-02-01 02:47

I\'m trying to replace all of the contents of an item in an observableArray with new content.

var oldLocation = ko.utils.arrayFirst(self.locations()         


        
3条回答
  •  情歌与酒
    2021-02-01 03:27

    I'm not aware of a replace method in JavaScript for arrays, or in Knockout. Am I missing something?

    If you want to use your second method, then you need to access locations as an observable:

    self.locations()[self.locations.indexOf(location)] = new fizi.ko.models.location(value);
    self.locations.valueHasMutated();
    

    though you don't when using indexOf, as there is a Knockout version of that for observable arrays.

提交回复
热议问题