用于空字符串验证的 Ngif angular 5

Ngif for empty string validation angular 5

我正在努力验证从服务器检索到的空字符串

它通常很简单,只是行不通

<div class="ui-g-2 info-txt"
     *ngIf="appointment.Notes !==null || 
     appointment.Notes !== ''">
     <i class="fa fa-commenting-o" aria-hidden="true"</i>
</div>

<div class="ui-g-5 info-txt"
     *ngIf="appointment.Notes !==null ||     
     appointment.Notes !== ''">
     *emphasized text*{{appointment.Notes}}
</div>

<div class="ui-g-7 info-txt"
     *ngIf="appointment.Notes === null || 
     appointment.Notes === ''"
     style="padding-top: 0;">
     no hay notas disponibles
</div>

所以我正在检查空值或空字符串,问题是它仍然显示图标和空字符串

当 no hay 里面有一条消息时......没有显示,所以它在那边工作得很好

任何帮助都非常感谢,多年来一直坚持这一点。

我确定它很明显我只是看不到它

不应该是:

appointment.Notes !==null && appointment.Notes !== ''"

您希望它不为 null 且不为空,但您只是在检查它是否不为 null 或不为空,二者兼而有之。这意味着,如果你有这个值:

const name = "";

*ngIf 条件为真,因为它不为空。

您可以使用双重否定来检查值是否不是 undefined/null 或 Empty。 例如:*ngIf="!!name".

这将防止显示所有 Null、未定义或空值。