diff --git a/src/app/app.component.html b/src/app/app.component.html index 9d9e954..736fd11 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,14 +1 @@ -
-
- 帳號: - - 帳號不存在 - -
-
密碼:
-
- - -
-
-
{{ form.value | json }}
+查詢條件: diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 9e27ab2..4b71ef0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,48 +1,37 @@ -import { Component } from '@angular/core'; +import { Component, OnDestroy, OnInit } from '@angular/core'; import { AbstractControl, FormBuilder, FormControl, ValidationErrors, } from '@angular/forms'; -import { Observable, of } from 'rxjs'; -import { map } from 'rxjs/operators'; +import { Observable, of, Subject, takeUntil } from 'rxjs'; +import { debounce, debounceTime, distinctUntilChanged, filter, map } from 'rxjs/operators'; -import { UserService } from './user.service'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: ['./app.component.css'], }) -export class AppComponent { - readonly form = this.fb.group({ - id: this.fb.control('', { - asyncValidators: [this.shouldBeExists], - updateOn: 'blur', - }), - password: this.fb.control(''), - }); - - get id(): FormControl { - return this.form.get('id') as FormControl; +export class AppComponent implements OnInit, OnDestroy { + readonly condition = new FormControl(); + readonly stop$=new Subject(); + ngOnInit():void{ + this.condition.valueChanges + .pipe( + debounceTime(500), + filter((condition:string)=> !!condition), + distinctUntilChanged(), + takeUntil(this.stop$) + ) + .subscribe({ + next:(condition) =>console.log(`查詢條件:${condition}`), + }); } - constructor(private fb: FormBuilder, private userSerivce: UserService) {} - - shouldBeExists( - control: AbstractControl - ): Observable { - if ( - control.value === undefined || - control.value === null || - control.value === '' - ) { - return of(null); - } - - return this.userSerivce - .isExists(control.value) - .pipe(map((isExists) => (isExists ? null : { shouldBeExists: true }))); + ngOnDestroy():void{ + this.stop$.next(); + this.stop$.complete(); } } diff --git a/src/app/user.service.ts b/src/app/user.service.ts deleted file mode 100644 index 27d4c9f..0000000 --- a/src/app/user.service.ts +++ /dev/null @@ -1,19 +0,0 @@ -import { HttpClient } from '@angular/common/http'; -import { Injectable } from '@angular/core'; -import { map, Observable, tap } from 'rxjs'; - -import { User } from './user'; - -@Injectable({ - providedIn: 'root', -}) -export class UserService { - constructor(private httpClient: HttpClient) {} - - isExists(id: string): Observable { - return this.httpClient.get('assets/data/user.json').pipe( - map((users) => !!users.find((user) => user.id === id)), - tap((user) => console.log(user)) - ); - } -} diff --git a/src/app/user.ts b/src/app/user.ts deleted file mode 100644 index 24bba39..0000000 --- a/src/app/user.ts +++ /dev/null @@ -1,4 +0,0 @@ -export class User { - id!:string; - password!:string; -}