angular - Inject AbstractControlDirective instance as @ContentChild to custom directive -
i have form control this:
<input formcontrolname="myname" dynamicdisable>
then custom directive:
@directive({ selector: '[formcontrolname][dynamicdisable] }) export class dynamicdisabledirective implements aftercontentinit { @contentchild(abstractcontroldirective) control: any; ngaftercontentinit(): void { console.log(this.control); } }
i inject directive owner element @contentchild directive. owner elements can type of form control, simple formcontrol, formgroup or formarray. formcontrolname descendant of abstractcontroldirective. why console logs 'undefined' then?
note: when switch abstractcontroldirective ngcontrol this:
@contentchild(ngcontrol) control: any;
everything works expected, form control instance gets injected , logged well. however, approach can used simple formcontrol, not groups , arrays.
as far know there nothing provided abstractcontroldirective
, therefore querying or injecting 1 not work without further assistance.
i haven't tried, might work register alias providers
@directive({ selector: '[formcontrolname][dynamicdisable], providers: [ { provide: abstractcontroldirective, useexisting: formcontrol, usemulti: true }, { provide: abstractcontroldirective, useexisting: formgroup, usemulti: true }, ] })
with usemulti: true
able (to of them), list if there more 1 match , filter list want.
Comments
Post a Comment