日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

给Angular应用增添搜索Search功能

發(fā)布時間:2023/12/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 给Angular应用增添搜索Search功能 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

(1) dashboard Component里增添搜索Component的selector:

使用命令行創(chuàng)建hero search Component:

ng generate component hero-search

(2) 實現(xiàn)這個search Component的ui:

<div id="search-component"><h4><label for="search-box">Hero Search</label></h4><input #searchBox id="search-box" (input)="search(searchBox.value)" /><ul class="search-result"><li *ngFor="let hero of heroes$ | async" ><a routerLink="/detail/{{hero.id}}">{{hero.name}}</a></li></ul></div>

注意第七行:

Notice that the *ngFor iterates over a list called heroes$, not heroes. The $ is a convention that indicates heroes$ is an Observable, not an array.

這里的heroes$不是一個數(shù)組,而是一個Observable.

Since *ngFor can’t do anything with an Observable, use the pipe character (|) followed by async. This identifies Angular’s AsyncPipe and subscribes to an Observable automatically so you won’t have to do so in the component class.

因為指令*ngFor不能直接同Observable打交道,因此使用管道| 和AsyncPipe.

(3) 實現(xiàn)search Component:

import { Component, OnInit } from '@angular/core';import { Observable, Subject } from 'rxjs';import {debounceTime, distinctUntilChanged, switchMap} from 'rxjs/operators';import { Hero } from '../hero'; import { HeroService } from '../hero.service';@Component({selector: 'app-hero-search',templateUrl: './hero-search.component.html',styleUrls: [ './hero-search.component.css' ] }) export class HeroSearchComponent implements OnInit {heroes$: Observable<Hero[]>;private searchTerms = new Subject<string>();constructor(private heroService: HeroService) {}// Push a search term into the observable stream.search(term: string): void {this.searchTerms.next(term);}ngOnInit(): void {this.heroes$ = this.searchTerms.pipe(// wait 300ms after each keystroke before considering the termdebounceTime(300),// ignore new term if same as previous termdistinctUntilChanged(),// switch to new search observable each time the term changesswitchMap((term: string) => this.heroService.searchHeroes(term)),);} }

要點分析:

第19行的searchTerms來自庫rxjs的Subject對象:

A Subject is both a source of observable values and an Observable itself. You can subscribe to a Subject as you would any Observable.

You can also push values into that Observable by calling its next(value) method as the search() method does.

將用戶輸入的term字符串變量放入searchTerms這個observable stream中。

如果每次用戶輸入的input事件都導(dǎo)致search函數(shù)執(zhí)行的話,將會產(chǎn)生大量的HTTP請求,因此此處引入一個限流機制:

  • debounceTime(300): waits until the flow of new string events pauses for 300 milliseconds before passing along the latest string. You’ll never make requests more frequently than 300ms. 新的input事件在300毫秒之后才會觸發(fā)。

  • distinctUntilChanged():ensures that a request is sent only if the filter text changed - 只有當(dāng)輸入發(fā)生變化時才觸發(fā)事件

  • switchMap() calls the search service for each search term that makes it through debounce() and distinctUntilChanged(). It cancels and discards previous search observables, returning only the latest search service observable.

取消和丟棄之前生成的observable,而使用當(dāng)前最新的observable進(jìn)行搜索。

實現(xiàn)效果:點擊搜索結(jié)果:

能夠跳轉(zhuǎn)到detail page:

要獲取更多Jerry的原創(chuàng)文章,請關(guān)注公眾號"汪子熙":

總結(jié)

以上是生活随笔為你收集整理的给Angular应用增添搜索Search功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 国产成年网站 | 久久免费福利视频 | 亚洲男人影院 | 婷婷色网| 中文字幕25页 | 国产福利合集 | 亚洲熟妇av乱码在线观看 | 在线激情av | 国产精品国产三级国产专播精品人 | 91在线视频导航 | 日本免费黄色网址 | 国产一级影片 | 天天射综合网站 | 啪啪网站大全 | 国产成人无码精品久久二区三 | 国产精品揄拍100视频 | 成年人免费在线观看网站 | 亚洲射射射 | 麻豆视频软件 | 日日插插 | 亚洲成人日韩在线 | 久久久久久九九九 | av在线不卡网站 | 久久偷看各类女兵18女厕嘘嘘 | 色欲久久久天天天综合网精品 | 午夜视频福利在线观看 | 欧美激情h | 超碰成人网| 在线视频黄 | 欧美不卡一二三 | www.色综合| 成年人免费在线观看视频网站 | 中文字幕亚洲图片 | 日韩成人自拍 | 无码精品人妻一二三区红粉影视 | 午夜av成人| 一级片小视频 | 国产美女引诱水电工 | 日本在线视频不卡 | 午夜裸体性播放 | 亚洲一区二区三区成人 | 久久99国产精品久久99 | 国产黄色在线播放 | 欧美高清hd | 快播av在线 | 182tv午夜福利在线观看 | 纯爱无遮挡h肉动漫在线播放 | 18欧美性xxxx极品hd | 91免费观看视频在线 | 最新亚洲精品 | 欧美激情一级精品国产 | 国产一区二区三区色淫影院 | 未满十八岁勿进 | 色九月婷婷| 亚洲国产97在线精品一区 | 欧美成人黑人猛交 | 欧美国产专区 | 亚洲综合情 | 一区二区精彩视频 | 成人拍拍拍 | 久久七 | 免费看的av网站 | 国产h片在线观看 | 狠狠干天天爱 | 黄色a在线 | 国产大片一区二区 | 久久视频在线播放 | 一级大片黄色 | 日本乱子伦xxxx | 99re99热 | 日本捏奶吃奶的视频 | 国产精品视频全国免费观看 | 久草福利免费 | 亚洲av久久久噜噜噜噜 | 看全黄大色黄大片 | 久久疯狂做爰流白浆xx | 国产激情无套内精对白视频 | 亚洲视频四区 | 久91 | 国产又猛又黄 | 不卡av网站| 日韩图片区| 日韩a级片在线观看 | 青草综合 | 91精品国产91久久久久福利 | 色www国产亚洲阿娇 自拍一区在线 | av官网在线| 香蕉网在线视频 | avwww.| 天海翼av | 邵氏电影《金莲外传2》免费观看 | 妺妺窝人体色WWW精品 | 韩国无码一区二区三区精品 | 亚洲高清精品视频 | 日本午夜小视频 | av九九九 | 欧美日韩三| 激情九九 | 黄色另类小说 |