angular - Is it possible to get native element for formControl? -


i've got angular2 reactive form. created formcontrols , 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(); } 

plunker example


Comments

Popular posts from this blog

unity3d - Rotate an object to face an opposite direction -

php - Doctrine: No alias was set before invoking getRootAlias() error -