Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular 4 : Can't bind to 'ngForFor' since it isn't a known property of 'li'

I've just started learning angular 4. This is a simple code that I'm trying to implement in Visual Studio Code, but keep getting this error.

Uncaught Error: Template parse errors:
Can't bind to 'ngForFor' since it isn't a known property of 'li'. ("
</ul>
<ul>
 <li [ERROR ->]*ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>"): ng:///AppModule/UserComponent.html@6:6
Property binding ngForFor not used by any directive on an embedded template. 
Make sure that the property name is spelled correctly and all directives are 
listed in the "@NgModule.declarations".("
</ul>
<ul>
[ERROR ->]<li *ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>"): ng:///AppModule/UserComponent.html@6:2

I tried previous solutions of adding the CommonModule to the app.module file. But it hasn't solved the issue.I cannot figure out what is wrong.

app.component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {

}

app.module.ts:

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


import { AppComponent } from './app.component';
import { UserComponent } from './components/user/user.component';
import {CommonModule} from '@angular/common';

@NgModule({
  declarations: [
    AppComponent,
    UserComponent
  ],
  imports: [
    BrowserModule,CommonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

user.component.ts:

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


@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.css']
})
export class UserComponent implements OnInit {
  name:string;
  age:number;
  address: Address;
  hobbies:string[];


  constructor() { 
    console.log('constructor ran ...');
  }

  ngOnInit() {
  console.log('ngOnInit ran ...');
  this.name='Raul';
  this.age=22;
  this.address= {
    street:'abc',
    city:'xyz',
    country: 'jkl'
  }
  this.hobbies=['reading','playing','swimming'];
  }

}

interface Address{
  street:string,
  city:string,
  country:string

}

user.component.html:

<h1>{{name}}</h1>
<ul>
  <li>Age:{{age}}</li>
  <li>Address:{{address.street}}, {{address.city}},{{address.country}}</li>
</ul>
<ul>
  <li *ngFor="let hobby for hobbies">{{hobby}}</li>
</ul>
like image 964
sa_1130 Avatar asked Dec 28 '17 16:12

sa_1130


People also ask

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

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.

Can't bind to ngFor since it isn't a known property of DIV in angular?

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.


1 Answers

should be of instead of for inside the ngFor

ngFor="let hobby of hobbies"
like image 64
Sachila Ranawaka Avatar answered Oct 20 '22 18:10

Sachila Ranawaka