我正在尝试查看页面内部.html文件 一些数据来自 API,这些数据只是通过 base64 进行长文本编码,问题是我想解码 html 文件中的文本而不是在 page.ts 文件中,所以我为此尝试了许多逻辑,但没有任何效果。HTML 文件中的示例:
- {
- {item.text|| base64}} {
- {item.text|| decodeURIComponent}} {
- {item.text|| atob}}
- atob({{item.text}})
但没有任何效果.在我尝试使用的 TS 页面内decodeURIComponent(escape(window.atob(ielement.text)))它正在工作,但我想要从 HTML 页面
这是来自 API 的文本:aGVsbG8gd29ybGQh
提前感谢..
您可以创建一个角度管道来解码 base64 字符串,如下所示:
import { Pipe, PipeTransform } from '@angular/core';
/*
* Usage:
* value | decodeBase64
* Example:
* {{ 'aGFyZXNo' | decodeBase64 }}
*
*/
@Pipe({name: 'decodeBase64'})
export class DecodeBase64Pipe implements PipeTransform {
transform(value: string): string {
return decodeURIComponent(escape(window.atob(value)));
}
}
并在 HTML 中按如下方式使用此管道:
<p>Decoded text: {{'aGFyZXNo' | decodeBase64}}</p>
您需要在 app.module 文件中添加管道才能注册。