Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't bind to 'ngforOf' since it isn't a known native property [duplicate]

Can't bind to 'ngforOf' since it isn't a known native property

    <h4>Colors bad boys</h4>
    <ul><li [ERROR ->]*ngfor="#color of colors">
        <span>{{color.color | upperCase}}</span>
      </li>

Trying to load the template via TemplateParser or RuntimeMetadataResolver and inject the overwtitten method in angular2 bootstrap e.g. for RuntimeMetadataResolver

The template loads its content correctly, but parsing fails with the error

Can't bind to 'ngforOf' since it isn't a known native property

like image 550
Vladimir Ivanov Avatar asked Feb 21 '16 00:02

Vladimir Ivanov


People also ask

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

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.

What is ngForOf?

Descriptionlink. The ngForOf directive is generally used in the shorthand form *ngFor . In this form, the template to be rendered for each iteration is the content of an anchor element containing the directive. The following example shows the shorthand syntax with some options, contained in an <li> element.

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.


1 Answers

Pay attention to the casing, it is *ngFor (capital F), not *ngfor or ng-for.

To fix your example, use:

...
<ul><li *ngFor="#color of colors">
...

Notice, again, it is *ngFor, not *ngfor.


Two plunkers showing the problem and correction:

Here's a plunker with that same error you have (look at app/app.component.ts and the console):

Can't bind to 'ngforOf' since it isn't a known native property

And then the exact same code, fixed, in this other plunker (just changed the F of *ngFor).


Lastest Angular versions

In recent versions of Angular we have @NgModules which require some additional configuration.

To get *ngFor, in your app module you have to import BrowserModule:

import { BrowserModule } from '@angular/platform-browser';

@NgModule({
  imports:      [ BrowserModule ],
  // ...
})
export class AppModule { }

In other modules, import CommonModule:

import { CommonModule } from '@angular/common';

@NgModule({
  imports:      [ CommonModule ],
  // ...
})
export class OtherModule { }
like image 133
acdcjunior Avatar answered Nov 15 '22 10:11

acdcjunior