diff --git a/src/app/app.component.html b/src/app/app.component.html index 258477c..f379fa6 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,15 +1,15 @@ - - - -
-
- -
工作事項:
-
工作內容:
- -
-
+
+
工作編號:{{ taskSn.value }}
+
+ 工作狀態: + +
+
+ 完成日期: + 完成日期為必填欄位
- -
{{ tasks.value | json }}
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index d50d3cf..b8e1237 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,49 +1,45 @@ -import { Component, OnDestroy, OnInit } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { - AbstractControl, - FormArray, FormBuilder, FormControl, - ValidationErrors, + FormGroup, + Validators, } from '@angular/forms'; -import { Observable, of, Subject, takeUntil } from 'rxjs'; -import { debounce, debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators'; - +import { map } from 'rxjs'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) -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) { } - - onSetValue(): void { - this.tasks.patchValue([ - { subject: 'Task A', content: 'Do something...' }, - { subject: 'Task B', content: 'Do something...' }, - ]) +export class AppComponent implements OnInit { + form: FormGroup = this.fb.group({ + TaskSn: this.fb.control('0001'), + Status: this.fb.control('Finish'), + FinishDate: this.fb.control(''), + }) + constructor(private fb: FormBuilder){} + get taskSn():FormControl{ + return this.form.get('TaskSn') as FormControl; } - - onAdd(): void{ - const group = this.fb.group({ - subject: this.fb.control(''), - content: this.fb.control(''), - }); - this.tasks.push(group); + get status(): FormControl{ + return this.form.get('Status') as FormControl; } - - onRemove(index: number): void { - this.tasks.removeAt(index); + get finishDate(): FormControl{ + return this.form.get('FinishDate') as FormControl; } - - onClear(): void { - this.tasks.clear(); + ngOnInit(): void { + this.status.valueChanges + .pipe(map((status) => status ==='Finish')) + .subscribe({ + next:(isFinish) =>{ + if(isFinish){ + this.finishDate.addValidators(Validators.required); + }else{ + this.finishDate.removeValidators(Validators.required); + } + this.finishDate.updateValueAndValidity(); + }, + }) } }