angular - 如何在Angular ngIf中检查SafeHtml类型是否为null
问题描述
如果 SafeHtml(query) 类型为空,我需要隐藏一个 div 元素。现在的问题是,如果我使用 *ngIf="query!=null" 它将不起作用。
从 '@angular/platform-browser' 导入 { DomSanitizer, SafeHtml };
解决方案
要检查Null或Empty String,您无需明确检查。只需检查变量内部*ngIf
条件。
请检查这个简短的例子:这里
TS
query: SafeHtml;
htmlString =
`<div style='background:gold'>
I am coming from controler with dynamic text -- <b> ${this.checkText} </b>
</div>`;
constructor(domSantizer: DomSanitizer) {
this.query = this.htmlString
? domSantizer.bypassSecurityTrustHtml(this.htmlString)
: '';
}
HTML
<div *ngIf="query">
<b> Check with SafeHtml </b>
<span [innerHTML]="query"> </span>
</div>
推荐阅读
- javascript - 在javascript中的url中传递一个变量
- java - 从 Mono.first 发出第一个成功的结果
- python - 在同一台服务器上查询具有相同表结构的多个数据库
- c# - 任何直接从序列化字符串创建 ClaimsIdentity 或从具有 json 数组类型的序列化字符串创建 ClaimsIdentity 的可能性
- python - Django 每个用户一个数据库还是一个一般数据库?
- equation - python pylatex 字符串中的行距、单位和数学方程
- r - 将列表列拆分为 R 数据帧上的多个整数列
- angular - 构建项目时 ngx-clipboard 出错
- labview - 通过一台远程计算机控制多个实验装置
- c++ - 如何在屏幕上显示经过时间的计数器,就像现实生活中的秒表一样?我对编程很陌生(C++)