|
@ -1,6 +1,7 @@ |
|
|
import { Component, OnDestroy, OnInit } from '@angular/core'; |
|
|
import { Component, OnDestroy, OnInit } from '@angular/core'; |
|
|
import { |
|
|
import { |
|
|
AbstractControl, |
|
|
AbstractControl, |
|
|
|
|
|
FormArray, |
|
|
FormBuilder, |
|
|
FormBuilder, |
|
|
FormControl, |
|
|
FormControl, |
|
|
ValidationErrors, |
|
|
ValidationErrors, |
|
@ -15,20 +16,34 @@ import { debounce, debounceTime, distinctUntilChanged, filter, map } from 'rxjs/ |
|
|
styleUrls: ['./app.component.css'], |
|
|
styleUrls: ['./app.component.css'], |
|
|
}) |
|
|
}) |
|
|
export class AppComponent { |
|
|
export class AppComponent { |
|
|
|
|
|
|
|
|
|
|
|
form = this.fb.group({ |
|
|
|
|
|
tasks: this.fb.array([]), |
|
|
|
|
|
}); |
|
|
|
|
|
get tasks(): FormArray { |
|
|
|
|
|
return this.form.get('tasks') as FormArray; |
|
|
|
|
|
} |
|
|
constructor(private fb: FormBuilder) { } |
|
|
constructor(private fb: FormBuilder) { } |
|
|
readonly form = this.fb.group({ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onSetValue(): void { |
|
|
|
|
|
this.tasks.patchValue([ |
|
|
|
|
|
{ subject: 'Task A', content: 'Do something...' }, |
|
|
|
|
|
{ subject: 'Task B', content: 'Do something...' }, |
|
|
|
|
|
]) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
onAdd(): void{ |
|
|
|
|
|
const group = this.fb.group({ |
|
|
subject: this.fb.control(''), |
|
|
subject: this.fb.control(''), |
|
|
content: this.fb.control(''), |
|
|
content: this.fb.control(''), |
|
|
}); |
|
|
}); |
|
|
|
|
|
|
|
|
onSetValue(): void { |
|
|
|
|
|
this.form.setValue({ subject: 'Task A', content: '' }); |
|
|
|
|
|
|
|
|
this.tasks.push(group); |
|
|
} |
|
|
} |
|
|
onPatchValue(): void { |
|
|
|
|
|
this.form.patchValue({subject:'Task A'}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onRemove(index: number): void { |
|
|
|
|
|
this.tasks.removeAt(index); |
|
|
} |
|
|
} |
|
|
onReset():void{ |
|
|
|
|
|
this.form.reset({subject:'', content:''}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
onClear(): void { |
|
|
|
|
|
this.tasks.clear(); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |