为什么 Angular 2 数组不会从列表中删除项目并需要重新加载



我有一个数组中的项目列表。我可以删除一个项目,只有在重新加载页面时它才会从列表中消失。该项目已成功从数据库中删除,但页面不会立即将其从列表中删除,因为我认为它应该这样做。

我没有显示任何错误,所以我可能错过了一些东西....

这是带来兴趣列表的方法:

 private uprofileInterests() {
        const query2 = {};
        query2['uprofileId.equals'] = this.uprofile.id;
        return this.interestService.query(query2).subscribe(
            (res: HttpResponse<IInterest[]>) => {
                this.interests = res.body;
                console.log('CONSOLOG: M:uprofileInterests & O: this.interests : ', this.interests);
            },
            (res: HttpErrorResponse) => this.onError(res.message)
        );
    }

这是删除项目的方法:

    removeProfileInterest(interestId, uprofileId) {
    this.interests.forEach(interest => {
        if (interest.id === interestId) {
            interest.uprofiles.forEach(uprofile => {
                if (uprofile.id === uprofileId) {
                    interest.uprofiles.splice(interest.uprofiles.indexOf(uprofile), 1);
                    this.subscribeToSaveResponse3(this.interestService.update(interest));
                }
            });
        }
    });
}

订阅保存响应3方法:

    private subscribeToSaveResponse3(result: Observable<HttpResponse<IInterest>>) {
    result.subscribe((res: HttpResponse<IInterest>) => this.onSaveSuccess2(), (res: HttpErrorResponse) => this.onSaveError());
}

以及 onSaveSuccess2 方法:

private onSaveSuccess2() {
    this.isSaving = false;
}

感谢您的帮助!

从列表中删除该项目,正如 Igor 在他的评论中所说:

this.interest.splice(interest.uprofiles.indexOf(uprofile), 1);

最新更新