angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例
本篇angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,分享給大家,具體如下:
添加一個(gè)pipe:
import { Pipe, Injectable, PipeTransform } from '@angular/core';
import { DomSanitizer } from '@angular/platform-browser';
@Pipe({
name: 'keyword'
})
@Injectable()
export class KeywordPipe implements PipeTransform {
constructor(private sanitizer: DomSanitizer) {
}
transform(val: string, keyword: string): any {
const Reg = new RegExp(keyword, 'i');
if (val) {
const res = val.replace(Reg, `<span style="color: #81E1B7;">${keyword}</span>`);
console.log(res);
return this.sanitizer.bypassSecurityTrustHtml(res);
}
}
}
注: DomSanitizer,這個(gè)的目的是是數(shù)據(jù)在頁(yè)面上的綁定能夠safe的解析
html中使用方法:
<ion-label [innerHTML]="item.name | keyword:searchText"></ion-label>
注: 在標(biāo)簽里面用新的標(biāo)簽包起來(lái),不然會(huì)有樣式問(wèn)題; 要用innerHTML來(lái)綁定數(shù)據(jù)。
演示效果

開(kāi)源項(xiàng)目地址:https://github.com/alex-0407/ionic3-awesome
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
AngularJs 60分鐘入門(mén)基礎(chǔ)教程
AngularJs是一個(gè)不錯(cuò)的用于開(kāi)發(fā)SPA應(yīng)用(單頁(yè)Web應(yīng)用)的框架。通過(guò)本文給大家介紹angularjs基礎(chǔ)教程,需要的朋友要求學(xué)習(xí)吧2016-04-04
將angular-ui的分頁(yè)組件封裝成指令的方法詳解
這篇文章主要給大家介紹了將angular-ui的分頁(yè)組件封裝成指令的方法,文中介紹的非常詳細(xì),相信會(huì)對(duì)大家的學(xué)習(xí)或者工作覺(jué)有一定的參考價(jià)值,需要的朋友下面來(lái)一起看看吧。2017-05-05
Angular應(yīng)用Bootstrap過(guò)程步驟邏輯詳解
這篇文章主要為大家介紹了Angular應(yīng)用Bootstrap過(guò)程步驟邏輯詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
angularjs自定義ng-model標(biāo)簽的屬性
這篇文章主要介紹了angularjs自定義ng-model標(biāo)簽的屬性的相關(guān)資料,需要的朋友可以參考下2016-01-01
AngularJS利用Controller完成URL跳轉(zhuǎn)
本文的主要內(nèi)容是介紹在AngularJS中怎樣利用Controller實(shí)現(xiàn)URL跳轉(zhuǎn),本文給出了實(shí)例代碼,簡(jiǎn)單明了,有需要的可以參考學(xué)習(xí)。2016-08-08

