小编典典

例外:无法绑定到“ngFor”,因为它不是已知的本机属性

all

我究竟做错了什么?

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

@Component({
  selector: 'conf-talks',
  template: `<div *ngFor="talk of 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 'ngFor' since it isn't a known native property
("<div [ERROR ->]*ngFor="talk of talks">

阅读 70

收藏
2022-04-06

共1个答案

小编典典

我错过let了前面talk

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

请注意,从beta.17#...开始,不推荐使用在结构指令(如
NgFor)中声明局部变量。改为使用let

<div *ngFor="#talk of talks">现在变成<div *ngFor="let talk of talks">

原答案:

我错过#了前面talk

<div *ngFor="#talk of talks">

很容易忘记这一点#。我希望 Angular 异常错误消息改为:
you forgot that # again.

2022-04-06