【问题标题】:how to use ngif properly in Angular如何在 Angular 中正确使用 ngif
【发布时间】:2020-05-27 13:25:11
【问题描述】:

我在 github 上找到了 angular 项目,我试图继续研究它以便我可以学习,但我一直坚持如何正确使用 ngIf。

所以我在 chart.model.ts 文件中有我的三种图表类型,我不确定谁可以访问我的 design.component.ts 文件中的数据,以便我可以使文本框仅在用户显示在下拉列表中选择文本。

现在,当我运行项目但没有执行任何操作时,三个图表类型选项会显示在下拉列表中,而且我在 ngIf 上也出现错误提示

“'ChartType' 类型上不存在属性 'text'。”

如果有人能教我或帮助我,我将不胜感激。

chart.model.ts

export class ChartUtil {
    public static getChartTypeDisplay(type: ChartType): string {
        switch (type) {
            case ChartType.chart:
                return "Charts";
            case ChartType.text:
                return "Text";
            case ChartType.grid:
                return "Grid";
            default:
                return "unknown";
        }
    }

export enum ChartType {
    chart = 1,
    text,
    grid
}
}

design.component.ts

import { ChartType } from 'src/app/chart.model';


 chartTypes: ChartType;


  setupTypes() {
    keys = Object.keys(ChartDataType);
    for (let i = 0; i < (keys.length / 2); i++) {
      this.dataTypes.push({ value: parseInt(keys[i]), display: ChartUtil.getChartDataTypeDisplay(parseInt(keys[i])) })
    }
}

design.component.html

        <ng-container *ngIf="chart.chartTypes == chartTypes.text">
            <mat-form-field appearance="fill">
                <mat-label>Text</mat-label>
                <input matInput />
            </mat-form-field>

我无法在 stackblitz 上上传完整的项目,但我通过 https://stackblitz.com/edit/angular-ivy-dmf3vn 上传了这些文件中的所有代码

【问题讨论】:

  • 你能说说 ChartType 模型包含什么吗?
  • @ZainZafar 嗨,我不太确定你的意思,但在 chart.model.ts 文件中我看到了这个,export class ChartConfig { type: ChartType; } 导出类 Chart{chartType: ChartType = CharType.chart } 并导出枚举 ChartType { chart = 1, text, grid }
  • 你也需要初始化chartTypes chartTypes: ChartType = ChartType.text。由于没有发布所有代码,这是最有意义的
  • 在您的组件中,您声明了一个 ChartType 类型的字段,它仅限制可以分配为枚举中的值。将其更改为chartTypes:ChartType = ChartType;并且您的 ngIf 应该可以工作。
  • @testpossessed 我做了,但不知道为什么我仍然收到错误。如果你们能从这些文件stackblitz.com/edit/angular-ivy-dmf3vn 中查看整个代码,我将不胜感激。谢谢

标签: html angular typescript angular-material angular-ng-if


【解决方案1】:

ng-If 的工作原理是,如果条件为真,则在 ng-if 中编写的代码将在 HTML 中呈现/显示。如果条件为假,则代码将不会被渲染/显示。因此,根据您的条件将值设置为 true 或 false。

 <ng-container *ngIf="displayChartInfo">
            <mat-form-field appearance="fill">
                <mat-label>Text</mat-label>
                <input matInput />
            </mat-form-field>

在你的 ts 中,

 displayChartInfo:boolean = false;
    public static getChartTypeDisplay(type: ChartType): string {
            switch (type) {
                case ChartType.chart:
                    this.displayChartInfo =true;;
                      break;
                case ChartType.text:
                     this.displayChartInfo =true;;
                      break;
                case ChartType.grid:
                     this.displayChartInfo =true;;
                      break;
                default:
                    this.displayChartInfo =false;;
                  break;

            }
        }

【讨论】:

  • 某些原因它说“属性 'displayChartInfo' 在类型 'typeof ChartUtil' 上不存在。”即使是我添加的那些“displayChartInfo: boolean = false;”在我的导出类 CharUtil 中。
  • 你可以调试并检查getChartTypeDisplay方法是否被调用?你能提供一个stackblitz的例子吗?
  • 嗨..我不认为我可以上传完整的项目,因为它包含很多文件和模块,但我已经上传了这些文件中的所有代码。如果你不介意请看这里stackblitz.com/edit/angular-ivy-dmf3vn谢谢
  • 你好,如果switch语句没有返回“Chart”,当我替换成this.displayChartInfo=true时这三个选项将如何显示。
猜你喜欢
  • 2019-11-09
  • 2020-10-03
  • 1970-01-01
  • 2019-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多