javascript - Angular 2 *ngFor 不工作

标签 javascript angular ngfor

以下是我的组件:

import { Component } from 'angular2/core';

@Component({
  selector: 'test',
  template: 
     `
      <ul >
        <li *ngFor="let t of test">
         <span >{{t}}</span>
        </li>
      </ul>
     `
})

export class TestComponent implements OnInit{
  test: string[];
  constructor(){
    this.test = ["Saab", "Volvo", "BMW"];
  }
}

尝试加载组件时出现以下错误:
  EXCEPTION: Template parse errors:
    Can't bind to 'ngFor' since it isn't a known native property ("<ul >
        <li [ERROR ->]*ngFor="let t of test">
            <span >{{t}}</span>
        </li>
    "): 

另外,我不确定在导入组件时是否应该使用“@angular/core”或“angular2/core”。

最佳答案

这个问题可能是因为你没有导入公共(public)模块
到您当前使用的模块。

尝试导入

import { CommonModule } from "@angular/common";

进入您当前的模块并查看问题是否已解决。

通用模块添加了 Angular 2 中的所有内置指令。

see commonModule documentation通过 angular.io

希望有帮助:)

关于javascript - Angular 2 *ngFor 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37822350/

相关文章:

javascript - 如何在 Python3 中创建带有倒数计时器的网页?

javascript - 为什么我的 html 按钮在 I.E 中移动?他们不会在 Chrome 或 FF 中移动

javascript - CSS 和 JS 文件未加载,MAMP 服务器

Angular 获取路由路径 - 未解析的 url

angular - 当最后一个输入有值时如何添加新的 &lt;input&gt; ?

javascript - 在 IE9 中打印 IFrame 文档

angular - HTTP 请求未触发

angular - Ionic 3 如何使用 textarea ngModel 和默认值?

angular - Angular *ngFor 中的 "="(等号)和 "of"有什么区别?

javascript - 将索引设置为模板 Angular 2 中的数字