Skip to content

Instantly share code, notes, and snippets.

@NyaGarcia
Last active June 10, 2022 13:33
Show Gist options
  • Save NyaGarcia/da0ed888f32cf2e58b2950247fb51ba4 to your computer and use it in GitHub Desktop.
Save NyaGarcia/da0ed888f32cf2e58b2950247fb51ba4 to your computer and use it in GitHub Desktop.
Creating the pokemon form dialog
import { Component, Inject, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
import { Pokemon } from '../../interfaces/pokemon.interface';
@Component({
selector: 'app-form',
templateUrl: './form.component.html',
styleUrls: ['./form.component.scss'],
})
export class FormComponent implements OnInit {
form: FormGroup;
constructor(
private readonly formBuilder: FormBuilder,
public readonly dialogRef: MatDialogRef<FormComponent>,
@Inject(MAT_DIALOG_DATA) private readonly pokemon: Pokemon
) {}
ngOnInit() {
this.setForm();
}
setForm() {
this.form = this.formBuilder.group({
name: [this.pokemon.name, [Validators.required]],
type: [this.pokemon.type, [Validators.required]],
description: [this.pokemon.description, [Validators.required]],
height: [this.pokemon.height, [Validators.required]],
weight: [this.pokemon.weight, [Validators.required]],
imgUrl: [this.pokemon.imgUrl, [Validators.required]],
});
}
submit() {
this.dialogRef.close({ ...this.pokemon, ...this.form.value });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment