且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

KnockoutJS从可观察数组中删除项目。项目是ul中的listitem,由foreach生成

更新时间:2023-12-02 20:21:10

当你从孩子那里调用一个方法时,将设置为子项而不是 $ parent

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

有很多方法可以确保使用适当的值调用 removeExpertise 。一种简单的方法是使用 .bind

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

它看起来像:

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

此外,您还需要专业知识一个 observableArray 而不是 observable ,作为 observableArray 公开数组操作方法,包括 remove 函数。

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