angular2/ionic2 實現(xiàn)搜索結果中的搜索關鍵字高亮的示例
本篇angular2/ionic2 實現(xià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>
注: 在標簽里面用新的標簽包起來,不然會有樣式問題; 要用innerHTML來綁定數(shù)據(jù)。
演示效果

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

