Skip to content

Instantly share code, notes, and snippets.

@SteGriff
Last active January 28, 2021 19:15
Show Gist options
  • Select an option

  • Save SteGriff/d80586d10259511d39f8febecdb29814 to your computer and use it in GitHub Desktop.

Select an option

Save SteGriff/d80586d10259511d39f8febecdb29814 to your computer and use it in GitHub Desktop.
Angular FormControls suck
ngOnInit(): void {
// Form field subscribes to self
// In FormGroup
this.anotherForm.controls.scoops.valueChanges.subscribe(value => {
this.anotherForm.controls.scoops.setValue(value, { onlySelf: true, emitEvent: false, emitModelToViewChange: true });
}, error => { }, () => { });
// Standalone FormControl
this.flavour.valueChanges.subscribe(value => {
this.flavour.setValue(value, { onlySelf: true, emitEvent: false, emitModelToViewChange: true });
}, error => { }, () => { });
}
<p>form-test works!</p>
<div class="mv2 pa1 ba">
<p>These two controls are bound to the same <code>formControl</code></p>
<label>Name:<input [formControl]="yourName" placeholder="Name"></label>
<label>Echo:<input [formControl]="yourName" placeholder="Name"></label>
<p>Value of Name: {{yourName.value}}</p>
</div>
<div class="mv2 pa1 ba">
<form [formGroup]="aForm">
<p>These two controls are bound to the same <code>formControlName</code> in a FormGroup</p>
<label>Age:<input formControlName="age" placeholder="Age"></label>
<label>Echo:<input formControlName="age" placeholder="Age"></label>
<p>Value of Age: {{aForm.controls.age.value}}</p>
</form>
</div>
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
@Component({
selector: 'app-form-test',
templateUrl: './form-test.component.html',
styleUrls: ['./form-test.component.css']
})
export class FormTestComponent implements OnInit {
yourName : FormControl = new FormControl('');
aForm = new FormGroup({
age: new FormControl()
});
constructor() { }
ngOnInit(): void {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment