[英]How can i access to my formControl which is injected in my component?
I have parent component.我有父组件。 Inside it i am projecting a input.
在它里面,我正在投射一个输入。
<app-form-field>
<input #input [id]="autoCompleteSearchInput" type="text" autoComplete="off"
class='input-underline search-bar idented-text' [placeholder]="placeholder"
[formControl]="search" />
</app-form-field>
So i am having this control所以我有这个控制
[formControl]="search"
now i can't find a way to access to the content injected formControl from my AppFormFieldComponent.现在我找不到访问从我的 AppFormFieldComponent 注入的内容的方法。
I tried with HTML我尝试使用HTML
<ng-content select=".input-underline"></ng-content>
TS TS
@ContentChild(FormControlName, {static: false}) formControl: FormControlName;
ngAfterContentInit() {
console.log(this.formControl);
}
but it gives me undefined.但它给了我未定义的。
How can i acess this form control so i can know what is typed inside input from my component ?我怎样才能访问这个表单控件,以便我可以知道在我的组件输入中输入了什么?
You are using [formControl]
so you should use FormControlDirective
in your @ContentChild
:你正在使用
[formControl]
所以你应该在你的@ContentChild
使用FormControlDirective
:
@ContentChild(FormControlDirective, {static: false})
formControl?: FormControlDirective;
ngAfterContentInit() {
console.log(this.formControl);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.