You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
1.1 KiB
41 lines
1.1 KiB
2 years ago
|
import { Component, OnInit } from "@angular/core";
|
||
|
import { FormArray, FormBuilder, FormGroup } from "@angular/forms";
|
||
|
import { FormValidators } from "@cdk/validators";
|
||
|
|
||
|
@Component({
|
||
|
selector: "app-food-form",
|
||
|
templateUrl: "./food-form.component.html",
|
||
|
styleUrls: ["./food-form.component.less"],
|
||
|
})
|
||
|
export class FoodFormComponent implements OnInit {
|
||
|
constructor(private fb: FormBuilder) {}
|
||
|
|
||
|
formGroup!: FormGroup;
|
||
|
|
||
|
get nutrition(): FormArray {
|
||
|
return this.formGroup.get("nutrition") as FormArray;
|
||
|
}
|
||
|
|
||
|
ngOnInit(): void {
|
||
|
this.formGroup = this.fb.group({
|
||
|
id: this.fb.control("", [FormValidators.required()]),
|
||
|
name: this.fb.control("", [FormValidators.required()]),
|
||
|
type: this.fb.control("", [FormValidators.required()]),
|
||
|
nutrition: this.fb.array([], [FormValidators.required()]),
|
||
|
});
|
||
|
}
|
||
|
|
||
|
createNutrition() {
|
||
|
this.nutrition.push(
|
||
|
this.fb.group({
|
||
|
nutritionName: this.fb.control("", [FormValidators.required()]),
|
||
|
nutritionNum: this.fb.control(0, [FormValidators.required()]),
|
||
|
})
|
||
|
);
|
||
|
}
|
||
|
|
||
|
removeNutrition(idx: number) {
|
||
|
this.nutrition.removeAt(idx);
|
||
|
}
|
||
|
}
|