角度2で@inputを使用して親コンポーネントから子コンポーネントにデータを通信します。目的は、値パラメータ@inputを使用して子コンポーネントでサービスを呼び出したいのですが、 valueプロパティは "undefine"です。これは私の子コンポーネントです:子コンポーネントで@inputプロパティを使用して角2でサービスを呼び出す
import { Component, OnInit, EventEmitter, Input,Output } from '@angular/core';
@Component({
selector: 'app-child',
templateUrl: './child.component.html'
})
export class ChildComponent implements OnInit {
@Input() productId: any; --> working perfectly if bind into html
constructor() {
}
ngOnInit(): void {
console.log(this.productId) --> undefine?
//in this methode i will call service to fetch another data from database using parameter productId..
}
}
これを解決するには?
なぜ定義する必要があると思いますか? 'productId'はどこから来たのですか?親コンポーネントからの –
、あなたの答えは完璧です@GünterZöchbauer – sembilanlangit
サーバからフェッチすると、 'ngOnInit()'が呼び出されたときにまだ利用できません。 setterまたは 'ngOnChanges'は後で設定されるデータに対しても機能します。 'ngOnInit()'は、コンポーネントの作成時に既に利用可能なデータに対してのみ機能します。 –