Home > Net >  How to add an ngModel and formControlName together in an angular custom component?
How to add an ngModel and formControlName together in an angular custom component?

Time:05-15

I want to make a custom component in angular .Sometimes i will use recative forms to call this component and sometimes without reactive components.

For that ,sometimes i will use formControlName and sometimes ngmodel. The problem is that i can not add those two properties together in my custom component.

ngmodel and custom component together

CodePudding user response:

So what you're looking for is the ControlValueAccessor interface: https://angular.io/api/forms/ControlValueAccessor

If you implement this interface on your component, the methods of the interface will allow the component to work with both template driven and reactive forms (ngModel and formControl/formControlName). For clarity, you don't need ngModel or formControlName as Inputs on your component, this interface will cause this to be allowed.

CodePudding user response:

Thanks to @Plabbee .It works like a charm.Just implement this interface ControlValueAccessor

  • Related