当使用 ngIf 和 Number 类型时,Angular 期望操作符具有相似类型或任何类型

标签 angular angular2-template

我有一个模型作为

class AnnouncementDocument {
  Id: String;
  IsDeleted: Boolean;
  Name: String;
  TemporaryName: String;
  Description: String;
  AnnouncementDocumentTypeId: Number;
}

在我使用的模板中

*ngIf="announcementDocumentTypeId==1

我得到了错误

[Angular] Expected the operants to be of similar type or any

这是对的,因为模板中的 1 被视为字符串而不是数字

我该如何解决这个问题?

最佳答案

要检查类型,你必须使用 === ,检查

*ngIf="announcementDocumentTypeId===1

关于当使用 ngIf 和 Number 类型时,Angular 期望操作符具有相似类型或任何类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46648027/

相关文章:

Angular 2 : Display different "toolbar" components depending on main navigation

angular - 有没有办法在 ngFor 中声明特定类型

javascript - Angular 6 路由重定向未正确重定向

Angular4 复制对象没有引用?

angular - p-文件上传 : how to read file after upload

angular - ngOnChanges Angular 2 不分配不同的值

angular - 将 Angular 5 与 ElasticSearch 连接时出现进程未定义错误?

javascript - Angular2 动态样式表元素

css - Angular 2 中的 ngClass 不在表格中呈现

angular - 无法捕获自定义事件