angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例
本篇angular2/ionic2 實(shí)現(xiàn)搜索結(jié)果中的搜索關(guān)鍵字高亮的示例,分享給大家,具體如下:
添加一個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,這個的目的是是數(shù)據(jù)在頁面上的綁定能夠safe的解析
html中使用方法:
<ion-label [innerHTML]="item.name | keyword:searchText"></ion-label>
注: 在標(biāo)簽里面用新的標(biāo)簽包起來,不然會有樣式問題; 要用innerHTML來綁定數(shù)據(jù)。
演示效果

開源項(xiàng)目地址:https://github.com/alex-0407/ionic3-awesome
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Ubuntu系統(tǒng)下Angularjs開發(fā)環(huán)境安裝
本文主要介紹 Ubuntu系統(tǒng)下Angularjs開發(fā)環(huán)境安裝,這里詳細(xì)介紹了安裝步驟和注意事項(xiàng),有在Ubuntu 環(huán)境下開發(fā)的朋友可以參考下2016-09-09
angular.js實(shí)現(xiàn)列表orderby排序的方法
今天小編就為大家分享一篇angular.js實(shí)現(xiàn)列表orderby排序的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-10-10
AngularJS 支付倒計(jì)時功能實(shí)現(xiàn)思路
這篇文章主要介紹了AngularJS 支付倒計(jì)時功能的實(shí)現(xiàn)思路,需要的朋友可以參考下2017-06-06
基于datepicker定義自己的angular時間組件的示例
這篇文章主要介紹了基于datepicker定義自己的angular時間組件,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
淺談Angular7 項(xiàng)目開發(fā)總結(jié)
這篇文章主要介紹了淺談Angular7 項(xiàng)目開發(fā)總結(jié),本文在此做一次遇到問題的總結(jié),以便知識的掌握,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
Angularjs實(shí)現(xiàn)多個頁面共享數(shù)據(jù)的方式
本文給大家介紹使用Angularjs實(shí)現(xiàn)多個頁面共享數(shù)據(jù)的方式,通過定義一個共享服務(wù)service來實(shí)現(xiàn)此功能,對angularjs共享數(shù)據(jù)相關(guān)知識感興趣的朋友一起學(xué)習(xí)2016-03-03

