How to replace a given index element in knockoutjs

后端 未结 5 610
时光说笑
时光说笑 2020-12-15 04:55

How do you replace a given index in an observableArray with another element.

I have: ViewModel.Elements()[index]

how can i replace it with anot

5条回答
  •  执念已碎
    2020-12-15 05:10

    observableArrays do have a replace method. This takes in the old item and the new item.

    So, you would call it like:

    ViewModel.Elements.replace(ViewModel.Elements()[index], yourNewElement);
    

    Internally, this just sets that index to your new item and calls valueHasMutated() to notify any potential subscribers.

提交回复
热议问题