Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 找不到对象。(定义对象时对象未定义)_Angular_Angular Material - Fatal编程技术网

Angular 找不到对象。(定义对象时对象未定义)

Angular 找不到对象。(定义对象时对象未定义),angular,angular-material,Angular,Angular Material,我正在学习Angular,我不明白为什么我不能从模板中的component.ts文件中获取对象的属性 我的代码: 从app.component.html <mat-form-field> <mat-label>Select an option</mat-label> <mat-select (click)="methood()" > <mat-option *ngFor="let color of colors"

我正在学习Angular,我不明白为什么我不能从模板中的component.ts文件中获取对象的属性

我的代码:

从app.component.html

<mat-form-field>
  <mat-label>Select an option</mat-label>
  <mat-select
  (click)="methood()" >
    <mat-option
    *ngFor="let color of colors"
    [value]="color.id">
      {{color.name}}
    </mat-option>
  </mat-select>
</mat-form-field>
它应该显示一个带有颜色的下拉列表,但它不显示。

您使用了a:而不是a=

你用了:而不是=


请检查typescript的基础知识以及如何赋值

您试图通过此语句分配:

colors: [
    { id: 1, name: 'Red' },
    { id: 2, name: 'Green' },
    { id: 3, name: 'Blue' }
  ];
虽然它必须是:

colors = [
    { id: 1, name: 'Red' },
    { id: 2, name: 'Green' },
    { id: 3, name: 'Blue' }
  ];


请检查typescript的基础知识以及如何赋值

您试图通过此语句分配:

colors: [
    { id: 1, name: 'Red' },
    { id: 2, name: 'Green' },
    { id: 3, name: 'Blue' }
  ];
虽然它必须是:

colors = [
    { id: 1, name: 'Red' },
    { id: 2, name: 'Green' },
    { id: 3, name: 'Blue' }
  ];


它在控制台中说什么?它说‘未定义的hello’它在控制台中说什么?它说‘未定义的hello’u来得太快:DFu来得太快:DF
colors: any[] = [ <--- or your type
    { id: 1, name: 'Red' },
    { id: 2, name: 'Green' },
    { id: 3, name: 'Blue' }
  ];