html - Angular:ngIf 无法识别枚举类型

标签 html css angular

这个*ngIf:

<div *ngIf="type === FilterType.DateRangeFilter">
    ...
</div>

导致错误

错误 TS2339:类型“FilterComponent”上不存在属性“FilterType”

即使枚举类型 FilterType 已导入组件中:

import { FilterType } from '../filter-collection/filter-collection.component'

@Component({
  selector: 'app-filter',
  templateUrl: './filter.component.html',
  styleUrls: ['./filter.component.css']
})
export class FilterComponent {

  constructor(private type : FilterType) {
  }

  ngOnInit(): void {

  }

}

从这里开始:

export enum FilterType {
  DateRangeFilter, SensorSelectFilter
}

有什么想法为什么这行不通吗?

最佳答案

您的 HTML 模板无权访问在组件外部声明的变量。要解决此问题,请将枚举分配给组件范围内的变量

import { FilterType } from '../filter-collection/filter-collection.component'

@Component({
  selector: 'app-filter',
  templateUrl: './filter.component.html',
  styleUrls: ['./filter.component.css']
})
export class FilterComponent {

  constructor(private type : FilterType) {
  }

  ngOnInit(): void {

  }
  
  filterType = FilterType

}

然后在您的模板中

<div *ngIf="type === filterType.DateRangeFilter">
    ...
</div>

关于html - Angular:ngIf 无法识别枚举类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68896744/

相关文章:

css - 固定元素在特定高度可见

jquery - 页眉、主页脚、页脚拉伸(stretch)以填充页面

angular - 如何使用在 Angular 6 中工作的 ngModel 创建自定义输入组件?

angular - primeNG p-multiSelect 具有动态 react 形式设置值

php - jQuery 自动填充表单无法工作

javascript - 为什么此 HTML 范围栏的值大于 1,而其最大值设置为 1?

Javascript 平滑滚动仍然滞后

Javascript if this OR this statement - 忽略第二个条件

html - firebug 如何识别 selenium 的唯一 css 路径?

javascript - Protractor :非 Angular 应用程序中的多个浏览器实例:ignoreSynchronization 不起作用