私は自分のservice.tsonEditItem()
でサブジェクトを定義しました.inはdetail.component.ts
idの値を渡し、
サブジェクト.next()
はnew.component.tsでサブスクライブされ
ますが、サブスクリプションは機能していません. サブスクリプションはnew.component.tsngOnInit
で行わ れます
id の値は正常に渡されましonEditItem()
たが、サブスクリプションが機能していません。サブスクリプション内で console.log チェックを試みました。しかし、コンソールには何も出力されません。
details.component.htmlで
<a class="btn btn-primary" (click)="onEditItem()" routerLink="/new">Edit</a>
details.component.tsで
onEditItem(){this.contactService.startedEditing.next(this.id);}
contactService.tsで
export class ContactService {
startedEditing =new Subject<number>();
}
new.component.tsで
ngOnInit() {
this.subscription = this.contactService.startedEditing.subscribe(
(index: number) => {
console.log(index);
this.editedItemIndex = index;
console.log(this.editedItemIndex);
this.editMode = true;
this.editedItem = this.contactService.getContacts(index);
this.editForm.setValue({
name: this.editedItem.name,
address: this.editedItem.address
})
}
);
}
で定義されているフォームをnew.component.html
詳細コンポーネントの値で初期化する必要があると思っていましたが、サブスクリプションが で機能していませんngOnInit
。