Skip to content

Instantly share code, notes, and snippets.

@bobbyg603
Last active September 6, 2022 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bobbyg603/1c0f765d9f4f36498764ba9492d72c44 to your computer and use it in GitHub Desktop.
Save bobbyg603/1c0f765d9f4f36498764ba9492d72c44 to your computer and use it in GitHub Desktop.
How to Build a Strongly Typed Angular 14 Super Form
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