I just started learning Angular 2 and I'm stuck on making common directives to work. I'm on Angular version 2.1.1
and I'm receiving the error Can't bind to 'ngforOf' since it isn't a known property of 'li'.
Same goes to *ngIf
and other directives.
Some of the most relevant tips I've read from similar questions here on making it work:
- Make sure to use the correct spelling and casing of the directives in the view, like so
ng-if
should be*ngIf
, etc.
- Make sure to use the correct spelling and casing of the directives in the view, like so
- Import
BrowserModule
in the root module andCommonModule
in other modules where you want to use common directives. Link
- Import
So far, here are parts of my code implementing those proposed fixes:
approot.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { HttpModule, RequestOptions } from '@angular/http';
import { AuthRequestOptions } from '../services/auth.request.options';
import { AppComponent } from '../approot/approot.component';
import { AppRoutingModule } from './approot.routing.module';
import { DashboardModule } from '../dashboard/dashboard.module';
@NgModule({
imports: [
BrowserModule,
HttpModule,
AppRoutingModule,
DashboardModule
],
declarations: [
AppComponent
],
providers: [
{provide: RequestOptions, useClass: AuthRequestOptions}
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
dashboard.module.ts
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DashboardRoutingModule } from './dashboard.routing.module';
import { DashboardComponent } from './dashboard.component';
import { DashboardHomeComponent } from './dashboard-home.component';
@NgModule({
imports: [
CommonModule,
DashboardRoutingModule,
],
declarations: [
DashboardComponent,
DashboardHomeComponent
],
})
export class DashboardModule { }
dashboard.component.ts (where I want to use *ngFor
)
import {Component} from "@angular/core";
import {ListService} from "../services/list.service";
import {ListModel} from "../models/list.model";
@Component({
templateUrl: '/views/dashboard/views/dashboard.html',
providers: [
ListService
],
styleUrls: ['../css/dashboard/styles/dashboard.css']
})
export class DashboardComponent {
lists: Array<ListModel>;
constructor (private listService: ListService) {
this.lists = [];
}
ngOnInit () {
console.log(this.lists);
this.listService.getLists().subscribe(
(data) => {
this.lists = data;
console.log(this.lists);
}
);
}
}
EDIT dashboard.html
<div class="row">
<div class="col-sm-3 col-md-2 sidebar">
<ul class="nav nav-sidebar">
<li class="active">
<a>My Lists</a>
</li>
<li *ngFor="let list of lists">
<a href="#">{{list.name}}</a>
</li>
</ul>
</div>
<div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
<router-outlet></router-outlet>
</div>
</div>
I also tried importing BrowserModule
instead of CommonModule
in dashboard.module.ts
but still got no luck.
I learned that previous angular2 beta versions has directive
attribute in the @Component()
and directives should be declared there so that they can be parsed. This is already deprecated in the current version of angular and I wonder if this has something to do with this error, angular not recognizing common directives.
Any help would be very much appreciated.