curiouswebster
asked on
Angular: Why is ngClass not working with my boolean?
I added a class to a div, and verify the booelan's working. But, the ng-class is failing.
Inside the Typescript template I have:
and the page does not generate any errors, and I verified the boolean values are properly set in ngOnInit()
What am I missing?
<div ng-class="{ 'dog-ear' : isDogEared }"></div>
Inside the Typescript template I have:
@Output() isDogEared:boolean;
dogEarIndexes: number[];
constructor()
{
this.dogEarIndexes = [ 1,3,5 ];
}
ngOnInit() {
this.imageFilename = "PuzzlePiece_Maps_" + (this.value.id + 1).toString() + ".gif";
this.id = this.value.id;
this.isDogEared = this.dogEarIndexes.indexOf(this.id) > -1;
console.log('Id:'+this.id+" - " + this.imageFilename + " isDogEared="+ this.isDogEared.toString());
}
and the page does not generate any errors, and I verified the boolean values are properly set in ngOnInit()
What am I missing?
ASKER
What is it that is specifically AnuglarJS(1.x)?
I added
import { NgClass } from '@angular/common';
but could not get either:
<div ng-class="{ 'dog-ear' : isDogEared }"></div>
nor
<div [NgClass]="{ 'dog-ear' : isDogEared }"></div>
to work.
I added
import { NgClass } from '@angular/common';
but could not get either:
<div ng-class="{ 'dog-ear' : isDogEared }"></div>
nor
<div [NgClass]="{ 'dog-ear' : isDogEared }"></div>
to work.
ng-class => Angular 1.x
[ngClass] => Angular 4
Here is my code that works
Puzzle Component HTML
[ngClass] => Angular 4
Here is my code that works
Puzzle Component HTML
<div class="puzzle-piece">
<img src="assets/images/{{value}}.jpg" />
<div [ngClass]="{'dog-ear': isDogEared}"></div>
</div>
Component tsimport { Component, OnInit, Output, Input } from '@angular/core';
@Component({
selector: 'puzzle-piece',
templateUrl: './puzzle-piece.component.html',
styleUrls: ['./puzzle-piece.component.css']
})
export class PuzzlePieceComponent implements OnInit {
@Input() value:String;
@Output() isDogEared:boolean;
dogEarIndexes: number[];
constructor() {
this.dogEarIndexes = [ 1,3,5 ];
}
ngOnInit() {
this.isDogEared = this.dogEarIndexes.indexOf(+this.value) > -1;
}
}
Canvas template<div class="puzzle">
<puzzle-piece *ngFor="let item of piece, let i = index" [value]="item" [pos]="i"></puzzle-piece>
</div>
Working sampler here
Note your use of @Output (which I copied in my code) is not quite correct. It should be an event Emitter not a boolean.
Rather make your boolean a normal property and if you need to send it up the hierarchy do so through an event emitter
Rather make your boolean a normal property and if you need to send it up the hierarchy do so through an event emitter
ASKER
I have a few questions about your code versus mine.
1)
I use:
@Input() value:PuzzlePieceComponent ;
but you use:
@Input() value:string;
isn't it better to be binding to the actual object type?
2)
I see you set a "pos" value in the HTML. But I do not see what I expected in the Typescript file:
@Input() pos:number;
1)
I use:
@Input() value:PuzzlePieceComponent
but you use:
@Input() value:string;
isn't it better to be binding to the actual object type?
2)
I see you set a "pos" value in the HTML. But I do not see what I expected in the Typescript file:
@Input() pos:number;
isn't it better to be binding to the actual object type?Depends on implementation - I am not using objects - my demo is just numbers in an array
I see you set a "pos" value in the HTML. But I do not see what I expected in the Typescript file:The sample I use is multi-purposed so sometimes things are left behind from previous samples.
Pos is one of them - it is not actually used and should be ignored in this context.
ASKER
I am trying to make my code look more like yours, but get an exception:
Uncaught Error: Template parse errors:
Can't bind to 'NgClass' since it isn't a known property of 'div'. ("<div class="puzzle-piece">
<img src="../../../assets/image s/{{ imageFilename }}" />
<div [ERROR ->][NgClass]="{ 'dog-ear' : isDogEared }"></div>
</div>"): ng:///AppModule/PuzzlePiec eComponent .html@2:9
I changed to use a string and removed @Output:
@Input() value:string;
isDogEared:boolean;
I get the same error with and without the following:
import { NgClass } from '@angular/common';
HTML
Uncaught Error: Template parse errors:
Can't bind to 'NgClass' since it isn't a known property of 'div'. ("<div class="puzzle-piece">
<img src="../../../assets/image
<div [ERROR ->][NgClass]="{ 'dog-ear' : isDogEared }"></div>
</div>"): ng:///AppModule/PuzzlePiec
I changed to use a string and removed @Output:
@Input() value:string;
isDogEared:boolean;
I get the same error with and without the following:
import { NgClass } from '@angular/common';
HTML
<div class="puzzle">
<puzzle-piece *ngFor="let piece of pieces, let i=index" [value]="piece" [id]="i"></puzzle-piece>
</div>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
You are welcome.
You need to use the Angular 4 version
Open in new window
Just an aside - you can style odd pieces using straight CSS