【问题标题】:Angular Material: selection list example not workingAngular Material:选择列表示例不起作用
【发布时间】:2019-05-20 08:10:30
【问题描述】:

我正在尝试使用 Angular Material (7.0) 选择列表 as described here

页面上的示例代码sn-p是

  <mat-selection-list #shoes>
    <mat-list-option *ngFor="let shoe of typesOfShoes">
      {{shoe}}
    </mat-list-option>
  </mat-selection-list>

  <p>
    Options selected: {{shoes.selectedOptions.selected.length}}
  </p>

在 TS 文件中定义 typesOfShoes,如他们的 sn-p 中所述:

export class HomeComponent implements OnInit {
  typesOfShoes: string[] = ['Boots', 'Clogs', 'Loafers', 'Moccasins', 'Sneakers'];
  constructor() {}
  ngOnInit() {}
}

对我来说很有意义,但是当我尝试编译时,我得到 错误

ERROR in: Can't bind to 'ngForOf' since it isn't a known property of 'mat-list-option'.
1. If 'mat-list-option' is an Angular component and it has 'ngForOf' input,
 then verify that it is part of this module.
2. If 'mat-list-option' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA'
 to the '@NgModule.schemas' of this component to suppress this message.
etc.

我已将 MatListModule 导入我的应用程序模块。

我没有看到与要导入的列表选项相关的其他模块。

我在这里错过了什么?

【问题讨论】:

标签: angular angular-material2


【解决方案1】:

@NgModule() 中导入BrowserModule(或CommonModule,如果它是子模块):

import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  imports: [BrowserModule],
})

【讨论】:

  • 我的应用模块已经有import { BrowserModule } from '@angular/platform-browser'。我的 sharedModule 导入 CommonModule 和我的 MaterialModule (包含所有材料模块的导入/导出)。我所有的其他材料组件都可以工作。
  • 实际上,我的共享模块导入了公共模块但没有导出它。这解决了它。
  • 今天这不起作用Module '"../../node_modules/@angular/common/common"' has no exported member 'BrowserModule'.ts(2305)
猜你喜欢
  • 2015-09-06
  • 1970-01-01
  • 2019-10-04
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-25
  • 2018-10-02
相关资源
最近更新 更多