|
@ -1,6 +1,10 @@ |
|
|
import { Component } from '@angular/core'; |
|
|
import { Component } from '@angular/core'; |
|
|
import { FormArray, FormControl, FormGroup } from '@angular/forms'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import { |
|
|
|
|
|
FormBuilder, |
|
|
|
|
|
FormControl, |
|
|
|
|
|
FormGroup, |
|
|
|
|
|
Validators, |
|
|
|
|
|
} from '@angular/forms'; |
|
|
|
|
|
|
|
|
@Component({ |
|
|
@Component({ |
|
|
selector: 'my-app', |
|
|
selector: 'my-app', |
|
@ -8,21 +12,33 @@ import { FormArray, FormControl, FormGroup } from '@angular/forms'; |
|
|
styleUrls: ['./app.component.css'], |
|
|
styleUrls: ['./app.component.css'], |
|
|
}) |
|
|
}) |
|
|
export class AppComponent { |
|
|
export class AppComponent { |
|
|
readonly form = new FormGroup({ |
|
|
|
|
|
Tasks: new FormArray([]), |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
// 方法一
|
|
|
|
|
|
// readonly form = new FormGroup({
|
|
|
|
|
|
// id: new FormControl('', [Validators.required]),
|
|
|
|
|
|
// password: new FormControl(''),
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
get tasks(): FormArray{ |
|
|
|
|
|
return this.form.get('Tasks') as FormArray; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// 方法二
|
|
|
|
|
|
// readonly form = new FormGroup({
|
|
|
|
|
|
// id: new FormControl('', { validators: [Validators.required] }),
|
|
|
|
|
|
// password: new FormControl(''),
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// 方法三
|
|
|
|
|
|
// readonly form = this.fb.group({
|
|
|
|
|
|
// id: this.fb.control('', [Validators.required]),
|
|
|
|
|
|
// password: this.fb.control('')
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// 方法四
|
|
|
|
|
|
readonly form = this.fb.group({ |
|
|
|
|
|
id: this.fb.control('', { validators: [Validators.required] }), |
|
|
|
|
|
password: this.fb.control(''), |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
ngOnInit(): void{ |
|
|
|
|
|
this.tasks.push(//注意此push用法
|
|
|
|
|
|
new FormGroup({ |
|
|
|
|
|
Subject: new FormControl(), |
|
|
|
|
|
Content: new FormControl(), |
|
|
|
|
|
}) |
|
|
|
|
|
); |
|
|
|
|
|
|
|
|
get id(): FormControl { |
|
|
|
|
|
return this.form.get('id') as FormControl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
constructor(private fb: FormBuilder) {} |
|
|
} |
|
|
} |