|
@ -1,28 +1,44 @@ |
|
|
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', |
|
|
templateUrl: './app.component.html', |
|
|
templateUrl: './app.component.html', |
|
|
styleUrls: ['./app.component.css'], |
|
|
styleUrls: ['./app.component.css'], |
|
|
}) |
|
|
}) |
|
|
export class AppComponent { |
|
|
|
|
|
readonly form = new FormGroup({ |
|
|
|
|
|
Tasks: new FormArray([]), |
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
export class AppComponent { |
|
|
|
|
|
// 方法一
|
|
|
|
|
|
// 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) {} |
|
|
} |
|
|
} |