简体   繁体   English

如何访问注入到我的组件中的 formControl?

[英]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.

相关问题 如何在我的FormControl数据中添加不是注入的数据模型所固有的额外键和值? - How can I add an extra key and value to my FormControl data that isn't native to the data model being injected? 我可以在 Angular 2+ 中访问我的自定义 ControlValueAccessor 的 formControl 吗? - Can I access to formControl of my custom ControlValueAccessor in Angular 2+? 在使用 Jasmine 进行单元测试时,我应该模拟或监视或存根注入我的组件中的服务吗? - While doing Unit testing with Jasmine should I mock or spy or stub my service which is injected in my component? 如何测试我所有的服务都被注入到组件中 - how to test all my Service are injected into component 如何从指令/组件访问ngModel的FormControl - How do I access an ngModel's FormControl from a Directive/Component 如何从组件中访问常量中的对象? - How can I access object from component in my constant? 如何访问我的内容投影元素? - How can i access to my elements which are content projected? 如何在子组件中获取FormControl值? - How can I get FormControl value in child component? 如何在 HTML 文档中使用 FromArray 中的特定 FormControl? - How can I use a specific FormControl from a FromArray in my HTML Document? 角度2:如何在该组件内访问自定义组件的FormControl实例? - Angular 2: How do I access a custom Component's FormControl instance within that Component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM