angular - Angular2 custom form control including textfield and error message -
i write custom form control including textfield , error message tooltip, works model-driven form.
at rc5, code following worked fine, @ 2.0, doesn't. https://embed.plnkr.co/bvfbsf5q74j7ehwuh5y7/
is there way establish relationship host formcontrol , child ngmodel, or access host formcontrol inner?
otherwise, there way realize idea?
i found way.
finally, code becomes this.
@component({ selector: 'custom-input', template: ` <input type="text" [required]="required" [(ngmodel)]="value" #model="ngmodel"/> <div>inner ngmodel errors: {{model.errors|json}}</div> <div>ngcontrol errors: {{ngcontrol.errors|json}}</div> `, providers: [{ provide: ng_value_accessor, useexisting: forwardref(() => custominput), multi: true }] }) export class custominput implements controlvalueaccessor, oninit { private innervalue: = ''; private ngcontrol: ngcontrol; @input() required: boolean; private ontouchedcallback = () => {}; private onchangecallback = (_: any) => {}; constructor(private injector: injector) { } ngoninit(): void { this.ngcontrol = this.injector.get(ngcontrol); } value(): { return this.innervalue; }; set value(v: any) { if (v !== this.innervalue) { this.innervalue = v; this.onchangecallback(v); } } onblur() { this.ontouchedcallback() } writevalue(value: any) { if (value !== this.innervalue) { this.innervalue = value; } } registeronchange(fn: any) { this.onchangecallback = fn } registerontouched(fn: any) { this.ontouchedcallback = fn } }
the host ngcontrol can got injector, injector.get(ngcontrol)
. , ngcontrol has errors
object want.
Comments
Post a Comment