私が呼び出したいメソッドを持っている子コンポーネントを作成しました。コール親クラスから子コンポーネント方法 - 角度
このメソッドを呼び出すと、console.log()
行だけが起動されますが、test
プロパティは設定されません。
以下は私の変化とクイックスタート角度アプリです。私もtest
プロパティを設定するにはどうすればよい
親
import { Component } from '@angular/core';
import { NotifyComponent } from './notify.component';
@Component({
selector: 'my-app',
template:
`
<button (click)="submit()">Call Child Component Method</button>
`
})
export class AppComponent {
private notify: NotifyComponent;
constructor() {
this.notify = new NotifyComponent();
}
submit(): void {
// execute child component method
notify.callMethod();
}
}
子供
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'notify',
template: '<h3>Notify {{test}}</h3>'
})
export class NotifyComponent implements OnInit {
test:string;
constructor() { }
ngOnInit() { }
callMethod(): void {
console.log('successfully executed.');
this.test = 'Me';
}
}
?
を子コンポーネントのメソッドを呼び出しますあなたのアプローチですが、直接使用しているときにエラーが発生していますives:[ChildCmp]、エラーメッセージ:指示は 'タイプ'コンポーネント 'に存在しません。私はそれを見つけたし、rc5ではディレクティブが非難されています。だから新しいバージョンでそれを処理する方法。助けてください。 –
このリンクhttps://angular.io/guide/component-interactionを試してみて、ディレクティブは、この行のChildVMは何 – rashfmnb