17

Given a component, with a form declaration

ngOnInit() {
  this.form = this.fb.group({
    address: [""],
  });
}

An two input controls on a form, both referencing the same control.

<input type="text" class="form-control" placeholder="Address" formControlName="address">
<input type="text" class="form-control" placeholder="Address" formControlName="address">

How do I keep the input values the same in each of the controls. Updating each input element does change the model value, but not the other corresponding input value. I am sure this is by design.

I am using the control on a tabbed interface, that requires a duplicate on each tab. Is there an easy way to keep them updated?

I have a working plunker demonstration.

2 Answers 2

33

just add a value field to the form

<input type="text" class="form-control" [value]="form.value.address" placeholder="Address" formControlName="address">
<input type="text" class="form-control" [value]="form.value.address" placeholder="Address" formControlName="address">

check out this plunker

Sign up to request clarification or add additional context in comments.

2 Comments

hah! thanks, can't believe it was so simple, and I missed it.
Cheers for that! Such a simple answer.
12

If the components have a reference to same form, but they are in different parts (different templates for example), you can subscribe the value change event to update the value. This works for me:

class CustomComponent implements OnInit {

 @Input() formGroup: FormGroup;

 ngOnInit() {

 this.formGroup.controls['param'].valueChanges.subscribe(x=> {
       this.formGroup.controls['param'].setValue(x, {onlySelf: true, emitEvent: false});
    });

  }

}   

So, if you modify the value in one component, the subscription will handle the changes in the other component. The emitEvent: false prevents to enter in a infinite loop on changes.

5 Comments

Thanks! I like this solution. Is onlySelf: true important? From documentation: "onlySelf: When true, each change only affects this control, and not its parent. Default is false." I did not use it, but maybe it's a good idea
It is a working solution but there is must another elegant one.
I have used the accepted answer in the past with success for string and number "ui syncing". However, this answer solved my problem for a Date object (for example bsDatePicker from ng-bootstrap)
Thanks, worked. I had to use it to sync two primeNg input components (slider + number input) that make up the input component together. "value" property is not available and "ngModel" is deprecated
Beware of this solution since it will cause an infinite loop. Since it is the same property which is accessed the emitEvent false and onlySelf true do not have any effect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.