Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

What am I doing wrong?

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, []) 

The error is

EXCEPTION: Template parse errors: Can't bind to 'ngForIn' since it isn't a known native property ("<div [ERROR ->]*ngFor="let talk in talks"> 
like image 561
Mark Rajcok Avatar asked Jan 01 '16 23:01

Mark Rajcok


People also ask

Can't bind to ngFor since it isn't a known property?

ngFor is not a known property would mean for whatever reason, ngFor cannot be recognized in the application. However, the real problem was that ngFor was recognised but the syntax of ngFor was wrong. Anyway, let's hope Angular team improves their logging in future updates.

Can't bind to ngIf since it isn't a known property of?

Solution for Angular Can't bind to 'ngIf' since it isn't a known property of 'div' There are multiple ways to fix this. Incorrect ngIf syntax One way, This is due to an error caused due to misspelling capital 'I' for div. To solve this, Import CommonModule or BroswerModule or both into app.

Can't bind to ngForOf since it isn'ta known property of Div angular 9?

Error: NG0303: Can't bind to 'ngForOf' since it isn't a known property of 'ion-item'. Explanation: This error occurs when you use ngIf/ ngFor in the component, where you have not imported CommonModule. Fix: To fix this you have to import the CommonModule in the module file which is used by that HTML file.


1 Answers

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> 
like image 105
Mark Rajcok Avatar answered Oct 17 '22 14:10

Mark Rajcok