2

私は自分のservice.tsonEditItem()でサブジェクトを定義しました.inはdetail.component.tsidの値を渡し、 サブジェクト.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

4

3 に答える 3