Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to get native element for formControl?

I've got Angular reactive form. I created formControls and assigned it to input fields by[formControl]=.... As I understand it creates nativeElement <-> formControl link.

My question: is it possible to get nativeElement for formControl? I want to do something like myFormControl.nativeElement.focus()

like image 413
fedor.belov Avatar asked Sep 22 '16 15:09

fedor.belov


People also ask

How do I get FormControl values?

To fetch the value of a form control, we have to use value property on the instance of FormControl in our class. In the same way we can fetch the value in HTML template. city = new FormControl('Noida'); console. log(this.

Can we use ngModel with FormControl?

ngModel and FormControls Don't Mix Support for using the ngModel input property and ngModelChange event with reactive form directives has been deprecated in Angular v6 and is scheduled for removal in a future version of Angular.

What does FormControl Reset do?

reset()linkResets the form control, marking it pristine and untouched , and resetting the value. The new value will be the provided value (if passed), null , or the initial value if nonNullable was set in the constructor via FormControlOptions .


2 Answers

The code below does not work with pure ngModel binding, so I did a lot of experiments. Latest, also confirmed by Maximillian Schwarzmuller should be the one:

@Directive({     selector: '[ngModel], [formControl]', // or 'input, select, textarea' - but then your controls won't be handled and also checking for undefined would be necessary }) export class NativeElementInjectorDirective {     constructor(private el: ElementRef, private control : NgControl, @Optional() private model : NgModel) {         if (!! model)             (<any>model.control).nativeElement = el.nativeElement;         else             (<any>control).nativeElement = el.nativeElement;     } } 

So if this directive is provided and exported in the main module, it will attach a custom nativeElement property to all FormControl.

It's a shame it's not coming out-of-the box...

like image 57
baHI Avatar answered Sep 20 '22 08:09

baHI


I can share one terrible solution but it works for me.

In reactive forms we 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 for these directives i could write some 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 that we can easily access to 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

like image 27
yurzui Avatar answered Sep 22 '22 08:09

yurzui