Last active
January 28, 2021 19:15
-
-
Save SteGriff/d80586d10259511d39f8febecdb29814 to your computer and use it in GitHub Desktop.
Angular FormControls suck
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 => { }, () => { }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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