Last active
September 6, 2022 13:13
-
-
Save bobbyg603/1c0f765d9f4f36498764ba9492d72c44 to your computer and use it in GitHub Desktop.
How to Build a Strongly Typed Angular 14 Super Form
This file contains 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, EventEmitter, Input, Output } from '@angular/core'; | |
import { FormControl, FormGroup } from '@angular/forms'; | |
import { Item } from '../cart/item'; | |
import { ControlsOf } from './controls-of'; | |
import { Shirt, Color, Size } from './shirt'; | |
@Component({ | |
selector: 'app-shirt', | |
templateUrl: './shirt.component.html', | |
styleUrls: ['./shirt.component.css'], | |
}) | |
export class ShirtComponent { | |
@Input() shirt!: Shirt; | |
@Output() addShirtToCartRequested = new EventEmitter<Item<Shirt>>(); | |
formGroup = new FormGroup<ControlsOf<Item<Shirt>>>({ | |
quantity: new FormControl(1), | |
item: new FormGroup({ | |
color: new FormControl(Color.Green as Color), | |
size: new FormControl(Size.medium as Size), | |
text: new FormControl('hello world 🌎'), | |
}), | |
}); | |
onSubmit(): void { | |
this.addShirtToCartRequested.emit(this.formGroup.getRawValue()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment