Search code examples
angularangular2-databindingangular-ng

Angular 2: Simple *ngFor not working


I have this in my app/component.ts

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

@Component({
  selector: 'app-root',
  template: `
    <ul>
      <li *ngFor="let course of courses">
        {{ course }}
      </li>
    </ul>
  `,
  styleUrls: ['./app.component.css']
})

export class AppComponent {
  course = ['Course 1', 'Course 2', 'Course 3'];
}

And in my app.module.ts, I also imported the necessary things

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

import { AppComponent } from './app.component';

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

This is just a simple *ngFor code but why is it not working. Aside from these, I have not touched anything


Solution

  • You have a typo, check in ur component definition. You defined course = [...]; but you use courses in your template. Try changing the variable name in the component to courses