【发布时间】:2016-03-17 16:29:20
【问题描述】:
我正在尝试在这里遵循基本的 Angular 2 教程:
https://angular.io/docs/js/latest/guide/displaying-data.html
我可以让 Angular 应用加载并使用以下代码显示我的名字:
import { Component, View, bootstrap } from 'angular2/angular2';
@Component({
selector: "my-app"
})
class AppComponent {
myName: string;
names: Array<string>;
constructor() {
this.myName = "Neil";
}
}
bootstrap(AppComponent);
但是,当我尝试添加一个字符串数组并尝试使用 ng-for 显示它们时,它会引发以下错误:
Can't bind to 'ng-forOf' since it isn't a known native property ("
<p>Friends:</p>
<ul>
<li [ERROR ->]*ng-for="#name of names">
{{ name }}
</li>
"): AppComponent@4:16
Property binding ng-forOf not used by any directive on an embedded template ("
<p>Friends:</p>
<ul>
[ERROR ->]<li *ng-for="#name of names">
{{ name }}
</li>
"): AppComponent@4:12
代码如下:
import { Component, View, bootstrap } from 'angular2/angular2';
@Component({
selector: "my-app"
})
@View({
template: `
<p>My name: {{ myName }}</p>
<p>Friends:</p>
<ul>
<li *ng-for="#name of names">
{{ name }}
</li>
</ul>
`,
directives: [ NgFor ]
})
class AppComponent {
myName: string;
names: Array<string>;
constructor() {
this.myName = "Neil";
this.names = ["Tom", "Dick", "Harry"];
}
}
bootstrap(AppComponent);
我错过了什么?
【问题讨论】:
-
有时是因为 IDE 中的插件失败(VS Code、Web Storm)。在这里查看我的答案:stackoverflow.com/a/70368326/4079915
标签: angular