Replacing item in observableArray

前端 未结 3 1340
半阙折子戏
半阙折子戏 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:26

    I simply want to mention an alternative way to do it:

    self.locations.splice(
      self.locations.indexOf(location),   // Index of the 1st element to remove
      1,                                  // Number of elements to remote at this index
      new fizi.ko.models.location(value)  // A param for each element to add at the index
    );
    

    Knockout includes splice in its documentation, but doesn't include replace: Knockout Obervable Arrays Docs. However, if you look at the source code you'll see that both functions are implemented (at least in KO 3.0, I don't know if replace was missing in previous versions).

提交回复
热议问题