Muhammad Ali
Muhammad Ali

Reputation: 369

Angular 5 autoFill form Keyup vs keydown vs keypress

Hi I am using reactive form and I have section there where I want to populate values into another field

now for some reason when I use saved values from the browser it does not fill the email completely. it only takes first letter of the email and this happens sometimes when I fill the email form manually.

here is the live example of my code.

https://stackblitz.com/edit/angular-5l9kb4

Upvotes: 0

Views: 191

Answers (1)

JossFD
JossFD

Reputation: 2216

Updated

fillForm() {
  this.myForm.valueChanges.subscribe(value => {
    //I'm using .controls and .get, but both work here. Pick the one you prefer
    this.myForm.controls['second_phone'].setValue(value.phone, { emitEvent: false});
    this.myForm.get('second_email').setValue(value.email, { emitEvent: false});
  });
}

You can also use { onlySelf: true } in the setValue if needed (Ref: Angular 2 - FormControl setValue 'onlySelf' parameter)

and remove: value="{{myForm.get('email').value}}" from your secondary fields in your templates.


Not the best approach

Try this:

fillForm() {
    this.myForm.valueChanges.subscribe(value => {
      if ((!value.second_email && value.email) || (!value.second_phone && value.phone)) {
          value.second_phone = value.phone;
          value.second_email = value.email;
      } 
    });
  }

Upvotes: 2

Related Questions