diff --git a/src/app/app.component.html b/src/app/app.component.html index 45bca65..258477c 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,11 +1,15 @@ -
-
工作事項:
-
工作內容:
-
- - - + + + +
+
+ +
工作事項:
+
工作內容:
+ +
+
-
{{form.value |json}}
-
{{form.dirty |json}}
+ +
{{ tasks.value | json }}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 38532ac..d50d3cf 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,6 +1,7 @@ import { Component, OnDestroy, OnInit } from '@angular/core'; import { AbstractControl, + FormArray, FormBuilder, FormControl, ValidationErrors, @@ -15,20 +16,34 @@ import { debounce, debounceTime, distinctUntilChanged, filter, map } from 'rxjs/ styleUrls: ['./app.component.css'], }) export class AppComponent { - - constructor(private fb: FormBuilder) { } - readonly form = this.fb.group({ - subject: this.fb.control(''), - content: this.fb.control(''), + form = this.fb.group({ + tasks: this.fb.array([]), }); + get tasks(): FormArray { + return this.form.get('tasks') as FormArray; + } + constructor(private fb: FormBuilder) { } onSetValue(): void { - this.form.setValue({ subject: 'Task A', content: '' }); + this.tasks.patchValue([ + { subject: 'Task A', content: 'Do something...' }, + { subject: 'Task B', content: 'Do something...' }, + ]) } - onPatchValue(): void { - this.form.patchValue({subject:'Task A'}); + + onAdd(): void{ + const group = this.fb.group({ + subject: this.fb.control(''), + content: this.fb.control(''), + }); + this.tasks.push(group); + } + + onRemove(index: number): void { + this.tasks.removeAt(index); } - onReset():void{ - this.form.reset({subject:'', content:''}); + + onClear(): void { + this.tasks.clear(); } }