|
|
@ -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(); |
|
|
|
} |
|
|
|
} |