KnockoutJS remove item from observable array. Item is listitem within ul, which was generated by foreach

前端 未结 1 621
再見小時候
再見小時候 2021-02-19 15:56

Using KnockoutJS, how can I remove an item from an observable array? I want to be able to click on the listitem, and remove the item from the array (and thereby the list).

相关标签:
1条回答
  • 2021-02-19 16:12

    When you call a method from the child, this will be set to the child rather than $parent.

    There are many ways to ensure that removeExpertise is called with the appropriate value for this. An easy way is to use .bind.

    It would look like:

    this.removeExpertise = function (expertise) {
        this.expertise.remove(expertise);
    }.bind(this);
    

    Also, you will want expertise to be an observableArray rather than an observable, as an observableArray exposes array manipulation methods including a remove function.

    0 讨论(0)
提交回复
热议问题