I am using a Subject to add edit functionality to a basic phonebook app. The changes that I make to component class variables inside of the subscribe callback are not reflected out of it or in the template.
I have tried the solutions listed in the answers to the following question: Angular 2 View will not update after variable change in subscribe
in service.ts:
startedEditing= new Subject<number>();
in details.component.ts(where the data is passed on fu):
onEdit(){
this.contactsService.startedEditing.next(this.id);
}
in edit-component.ts(in ngOnInit):
this.subscription=this.contactsService.startedEditing.subscribe(
(index:number)=>{
this.editMode=true;
this.editedItemIndex=index;
this.editItem=this.contactsService.getContact(this.editedItemIndex);
this.editForm.setValue({
address:this.editItem.address,
name: this.editItem.name,
phone:this.editItem.phone,
profession:this.editItem.profession
});
console.log("in subscribe: "+this.editedItemIndex);
}
);
console.log("out of it :" + this.editedItemIndex);
}
output on the console:
in subscribe: 0
out of it : undefined
expected result:
in subscribe: 0
out of it :0