Angular exception: Can't bind to 'ngForIn' since it isn't a known native property

前端 未结 9 2262
说谎
说谎 2020-11-30 21:15

What am I doing wrong?

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

@Component({
  selector: \'conf-talks         


        
相关标签:
9条回答
  • 2020-11-30 21:39

    My problem was, that Visual Studio somehow automatically lowercased *ngFor to *ngfor on copy&paste.

    0 讨论(0)
  • 2020-11-30 21:40

    Since this is at least the third time I've wasted more than 5 min on this problem I figured I'd post the Q & A. I hope it helps someone else down the road... probably me!

    I typed in instead of of in the ngFor expression.

    Befor 2-beta.17, it should be:

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

    As of beta.17, use the let syntax instead of #. See the UPDATE further down for more info.


    Note that the ngFor syntax "desugars" into the following:

    <template ngFor #talk [ngForOf]="talks">
      <div>...</div>
    </template>
    

    If we use in instead, it turns into

    <template ngFor #talk [ngForIn]="talks">
      <div>...</div>
    </template>
    

    Since ngForIn isn't an attribute directive with an input property of the same name (like ngIf), Angular then tries to see if it is a (known native) property of the template element, and it isn't, hence the error.

    UPDATE - as of 2-beta.17, use the let syntax instead of #. This updates to the following:

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

    Note that the ngFor syntax "desugars" into the following:

    <template ngFor let-talk [ngForOf]="talks">
      <div>...</div>
    </template>
    

    If we use in instead, it turns into

    <template ngFor let-talk [ngForIn]="talks">
      <div>...</div>
    </template>
    
    0 讨论(0)
  • 2020-11-30 21:40

    Watching this course https://app.pluralsight.com/library/courses/angular-2-getting-started-update/discussion

    The author explains that new version of JavaScript has for of and for in, the for of is to enumerate objects and the for in is to enumerate the index of the array.

    0 讨论(0)
提交回复
热议问题