加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

Angular2(最终版本)* ngFor in component:无法绑定到’ngForOf

发布时间:2020-12-17 09:03:40 所属栏目:安全 来源:网络整理
导读:参见英文答案 *ngFor is not working in angular 21个 我开始使用Angular2(最终版本),我在使用* ngFor时遇到了一些问题. 我已经构建了以下组件树: 主要 – 仪表板 – 警报 Unhandled Promise rejection: Template parse errors: Can’t bind to ‘ngForOf’
参见英文答案 > *ngFor is not working in angular 21个
我开始使用Angular2(最终版本),我在使用* ngFor时遇到了一些问题.

我已经构建了以下组件树:
主要 – >仪表板 – >警报

Unhandled Promise rejection: Template parse errors: Can’t bind to
‘ngForOf’ since it isn’t a known property of ‘div’.

(“<div class=”item” [ERROR ->] *ngFor=”let alert of alerts”>

“): DashboardAlertsComponent@5:20 Property binding ngForOf not used by any directive on an embedded template.

Make sure that the property name is spelled correctly and all
directives are listed in the “directives” section.

它工作正常,但当我尝试在警报组件中添加* ngFor循环时,我收到以下错误:

DashboardAlertsComponent:

import {Component,OnInit} from "@angular/core";
import {Alert} from "../../shared/models/alert.model";

@Component({
  selector: 'dashboard-alerts',templateUrl: './alerts.component.html'
})
export class DashboardAlertsComponent implements OnInit {

  alerts:Alert[] = new Array<Alert>();
  constructor() {
    this.alerts.push(new Alert('1','high','My alert1','12/11/2016 4:50 PM'));
    this.alerts.push(new Alert('2','low','My alert2','11/11/2016 9:50 PM'));
    this.alerts.push(new Alert('3','medium','My alert3','10/11/2016 2:50 PM'));
  }

  ngOnInit() {
  }
}

alerts.component.html

<div class="item" *ngFor="let alert of alerts">
    <span class="priority {{ alert }}"></span>
    <span class="title">{{ alert }}</span>
    <span class="time">3:40 PM</span>
    <a href="#" class="more-actions"><span>Actions</span></a>
</div>

DashboardModule

@NgModule({
  declarations: [
    DashboardAlertsComponent
  ],providers: [],directives: [],})
export class DashboardModule {
}

的AppModule

@NgModule({
  imports: [
    BrowserModule,DashboardModule
  ],declarations: [
    AppComponent
  ],bootstrap: [AppComponent]
})
export class AppModule {
}

P.S我为这个问题阅读了许多建议的解决方案,但没有一个解决了这个问题

您应该在主模块中导入CommonModule,在其他模块中导入BrowserModule(如果您正在使用多个模块).我遇到了同样的问题,对我来说效果很好

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读