我做错了什么?

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">

当前回答

在我的例子中,我犯了从文档中复制*ng-for=的错误。

https://angular.io/guide/user-input

如果我错了,请指正。但是*ng-for=似乎已经被更改为*ngFor=

其他回答

在我的例子中,我没有在for循环中声明循环变量

<div *ngFor="pizza of pizzas; index as i">

而不是

<div *ngFor="let pizza of pizzas; index as i">

在声明了“let”之后,它就对我有用了。

也不要尝试使用纯TypeScript…我想更对应的用法和使用*ngFor="const filter of filters"和得到的ngFor不是一个已知的属性错误。只要用let替换const就可以了。

正如@alexander-abakumov所说的,of被in取代。

此语句用于Angular2 Beta版本.....

以后用let代替#

Let关键字用于声明局部变量

再讲一种情况当我得到同样的错误时原因是用in而不是in迭代器

错误的方式让文件进入文件

正确的方式让文件的文件

我忘记用“@Input”来注释我的组件(哎呀!)

html(违规代码):

<app-book-item
  *ngFor="let book of book$ | async"
  [book]="book">  <-- Can't bind to 'book' since it isn't a known property of 'app-book-item'
</app-book-item>

book-item.component.ts的修正版本:

import { Component, OnInit, Input } from '@angular/core';

import { Book } from '../model/book';
import { BookService } from '../services/book.service';

@Component({
  selector: 'app-book-item',
  templateUrl: './book-item.component.html',
  styleUrls: ['./book-item.component.css']
})
export class BookItemComponent implements OnInit {

  @Input()
  public book: Book;

  constructor(private bookService: BookService)  { }

  ngOnInit() {}

}