以下是我的组件:
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/