Mike
Mike

Reputation: 1097

Why Angular 2 array does not remove an item from the list and needs to reload?

I have list of items in an array. I can remove one item and it only disappears from the list when the page is reloaded. The item is successfully removed from the database but the page does not removes it from the list instantaneously, as I suppose that it should do.

I shows no errors, so I might be missing something....

This is the method that brings the list of interests:

 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)
        );
    }

And this is the method that removes an item:

    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));
                }
            });
        }
    });
}

The subscribeToSaveResponse3 method:

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

And the onSaveSuccess2 method:

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

Thanks for your help!

Upvotes: 0

Views: 48

Answers (1)

Jorge M. Nures
Jorge M. Nures

Reputation: 692

Remove the item from the list as Igor is saying in his comments:

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

Upvotes: 1

Related Questions