我有以下组件:
@Component({
selector: "form-component",
template: ``
})
export class FormComponent {
@Input() userInput?: string;
}
现在我想通过编写将成员userInput
(我总是使 Input 绑定可选,因为它们可能不会被使用)转换为 a :FormControl
@Input() userInput = new FormControl("");
或者这是否与绑定机制有任何冲突?关于类型,这似乎不是一个好习惯,因为不再userInput
是string
。
我的问题
如何将@Input
绑定分配给 a 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 : "");
}
}