我做错了什么?

import {bootstrap, Component} from 'angular2/angular2'

@Component({
  selector: 'conf-talks',
  template: `<div *ngFor="let talk in talks">
     {{talk.title}} by {{talk.speaker}}
     <p>{{talk.description}}
   </div>`
})
class ConfTalks {
  talks = [ {title: 't1', speaker: 'Brian', description: 'talk 1'},
            {title: 't2', speaker: 'Julie', description: 'talk 2'}];
}
@Component({
  selector: 'my-app',
  directives: [ConfTalks],
  template: '<conf-talks></conf-talks>'
})
class App {}
bootstrap(App, [])

错误是

EXCEPTION: Template parse errors:
Can't bind to 'ngForIn' since it isn't a known native property
("<div [ERROR ->]*ngFor="let talk in talks">

当前回答

我的解决方案是-只需从表达式^__^中删除'*'字符

<div ngFor="let talk in talks">

其他回答

简单地把“in”改成“of”就解决了同样的问题。

Chnage <div *ngFor="let talk in talks">

to     <div *ngFor="let talk of talks">

为了增加更多的问题,我天真地发现,如果你不使用let语句来分配循环的索引,你也会得到同样的错误:

<span *ngFor="let button of buttons; i: index; trackBy: trackBy">
...
</span>

也抛出相同的错误。下面的版本修复了它

<span *ngFor="let button of buttons; let i = index; trackBy: trackBy">
...
</span>

如果你想使用of而不是切换到in,还有另一种选择。您可以使用6.1中介绍的KeyValuePipe。你可以很容易地迭代一个对象:

<div *ngFor="let item of object | keyvalue">
  {{item.key}}:{{item.value}}
</div>

用of代替in你可以使用KeyValue Pipe。你可以很容易地迭代一个对象:

<div *ngFor="let items of allObject | keyvalue">
  {{items.key}}:{{items.value}}
</div>

尝试从'@angular/common'导入import {CommonModule};angular final中的*ngFor,*ngIf都存在于CommonModule中