次のコンポーネントがあります。
@Component({
selector: "form-component",
template: ``
})
export class FormComponent {
@Input() userInput?: string;
}
そして今、私はメンバーを変換したいと思っていuserInput
ます (私は常に入力バインディングをオプションにします。使用されない可能性があるためです) FormControl
。
@Input() userInput = new FormControl("");
または、これは何らかの方法でバインディングメカニズムと競合しますか? 型に関しては、これは良い習慣でuserInput
はないようstring
です。
私の質問
@Input
プロパティにバインディングを割り当てるにはどうすればよいFormControl
ですか?
提案
次のように onInit に (おそらく) バインドされた値を割り当てる必要があるかもしれません。
@Component({
selector: "form-component",
template: ``
})
export class FormComponent implements OnInit{
@Input() userInput?: string;
userControl: FormControl;
ngOnInit() {
this.userControl = new FormControl(this.userInput ? this.userInput : "");
}
}