I am following Template syntax section for the application of built-in attribute and structural directives from here https://v4.angular.io/guide/template-syntax#ngclass
currentClasses: {};
setCurrentClasses() {
// CSS classes: added/removed per current state of component properties
this.currentClasses = {
'saveable': this.canSave,
'modified': !this.isUnchanged,
'special': this.isSpecial
};
}
I add currentClasses to my html as per Angular documentation above:
<div [ngClass]="currentClasses">This div is initially saveable, unchanged, and special</div>
On browser's console I am getting the following error:
Error: Template parse errors:
Can't bind to 'ngclass' since it isn't a known property of 'div'.
I tried also ngStyle and ngIf but getting the same error code.
My app.module.ts includes Common Module as this was the suggested solution in some of the answers - but this has not helped me:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { AppComponent } from './app.component';
import { MaterialModule } from './material-module';
import { HighlightDirective } from './highlight.directive';
import { BrowserAnimationsModule } from '@angular/platform
browser/animations';
import { PlatformModule } from '@angular/cdk/platform';
import { BreakpointObserver, MediaMatcher } from '@angular/cdk/layout';
// import { MediaService } from './Media.service';
@NgModule({
declarations: [
AppComponent,
HighlightDirective
],
imports: [
CommonModule,
BrowserModule,
BrowserAnimationsModule,
MaterialModule,
PlatformModule
],
providers: [BreakpointObserver, MediaMatcher],
bootstrap: [AppComponent]
})
export class AppModule { }
I could not make it a work by myself and also after following some other answers given to similar questions.
I am using Angular 4.4.6 and webpack to compile.
Thanks very much for any help.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…