最佳答案
我试着在这里学习角度2的基本教程:
Https://angular.io/docs/js/latest/guide/displaying-data.html
我可以让角度应用程序载入并显示我的名字,代码如下:
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);
我错过了什么?