angular - 在 Ionic 中移除 HTML 標籤?
使用 Ionic 3,我已從 WordPress API 獲取了一些數據,然後將其顯示在應用程序界面上。除內容中包含的 HTML 標籤外,一切似乎運行正常。HTML 標籤也被打印出來。我參考了一些資源,它們建議了以下代碼:-
`var app = angular.module('myHDApp', []);
app.filter('removeHTMLTags', function() {
return function(text) {
return text ? String(text).replace(/<[^>]+>/gm, '') : '';
};
});
我已經在我的 .ts 代碼中實現了上述函數,但它似乎對我無效,因為我仍然在內容中看到 HTML 標籤。
答案#1
你找到的是針對 ionic v1 的。在 ionic 3 中,你必須先創建一個管道。
在你的命令列介面中,
ionic g pipe removehtmltags
你可以在 src/pipes 下找到你新創建的管道。現在在你的 removehtmltags.ts 中,
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'removehtmltag',
})
export class RemovehtmltagPipe implements PipeTransform {
/**
* Takes a value and makes it lowercase.
*/
transform(value: string) {
if(value){
var result = value.replace(/<\/?[^>]+>/gi, ""); //removing html tag using regex pattern
return result;
}
else{}
}
}
你現在可以在你的 HTML 文件中像這樣使用這個管道,
<p>{{yourData | removehtmltag}}</p>
答案#2
最簡單的解決方法:
removeHTMLInfo(value: string)
{
if (value)
return value.replace(/<\/?[^>]+>/gi, "");
}
