angular - Is it possible to get native element for formControl? -
i've got angular2 reactive form. created formcontrol
s , assigned input fields by[formcontrol]=...
. understand creates nativeelement <-> formcontrol
link.
my question: possible nativeelement
formcontrol
? wanna myformcontrol.nativeelement.focus()
i can share 1 terrible solution works me.
in reactive forms can use either
1) formcontroldirective
ts
mycontrol = new formcontrol('')
template
<input type="text" [formcontrol]="mycontrol">
or
2) formcontrolname
ts
myform: formgroup; constructor(private fb: formbuilder) {} ngoninit() { this.myform = this.fb.group({ foo: '' }); }
template
<form [formgroup]="myform"> <input type="text" formcontrolname="foo"> </form>
so these directives write patch like
1) formcontroldirective
const originformcontrolngonchanges = formcontroldirective.prototype.ngonchanges; formcontroldirective.prototype.ngonchanges = function() { this.form.nativeelement = this.valueaccessor._elementref.nativeelement; return originformcontrolngonchanges.apply(this, arguments); };
2) formcontrolname
const originformcontrolnamengonchanges = formcontrolname.prototype.ngonchanges; formcontrolname.prototype.ngonchanges = function() { const result = originformcontrolnamengonchanges.apply(this, arguments); this.control.nativeelement = this.valueaccessor._elementref.nativeelement; return result; };
after can access native element having formcontrol
instance
1) formcontroldirective
focustoformcontrol() { (<any>this.mycontrol).nativeelement.focus(); }
2) formcontrolname
focustoformcontrolname(name) { (<any>this.myform.get(name)).nativeelement.focus(); }
Comments
Post a Comment