1
ngAfterViewInitでフォームの値を取得しようとしています。残念ながら、それは常に空です。私は{"name1":null, "name2": null}
のようなものを経験していますが、私は{ }
を得ています。なぜngAfterViewInitでform.valueが空であるか
ドキュメントを約ngAfterViewInit
次の言葉:コンポーネントのビューと子ビューを初期化した後に呼び出さ
。
私はこの時点でフォームが作成されたと仮定していますが、なぜ私の値は空ですか?
私のhtml:
<form novalidate #form="ngForm">
// <Item 1...
// <Item 2...
<button type="submit" (click)="search()" class="btn btn-default">Zoeken</button>
</form>
//This is displaying the correct value
<div>
<p>Form: {{ form.value | json }}</p>
<p>Form status: {{ form.control.status }}</p>
</div>
コンポーネント:
export class BijComponent implements AfterViewInit, OnInit {
@ViewChild('form') form: FormGroup;
constructor(private bijService: BijService, private sessionService: SessionService) { }
ngOnInit() {
// Do stuff
}
ngAfterViewInit() {
let v = JSON.stringify(this.form.value);// is `{ }`???
}
search(){
//Search function
}
}
これはなぜ 'ngAfterViewInit'には存在しませんが、' search'関数には存在しますか? –
「検索機能に存在する」とはどういう意味ですか? 'search()'の中で 'console.log(this.form.value)'にしようとしましたか? – Supamiu
はい、あります。 '{" name1 ":null、" name2 ":null}'を返します。しかし、 'ngAfterViewInit'や他のいくつかのonCreateメソッドでその値が必要です –