1

I get the object like

{ name: 'filename', order: '', open: true, isEditOn: false, tempVal: '', id: 0}

when I iterate an array of objects... Now I want to add the class of the div like

obj.filename + obj.id

I have tried like [ngClass] = "{obj.filename + obj.id}"..

4
  • Could you please add whatever code you have tried? Commented Jan 13, 2020 at 9:09
  • You need use [className]="obj.filename+obj.id", [ngClass] is for asing class if a condition is satisf, e.g. `[ngClass]="{'classname':condition}" Commented Jan 13, 2020 at 9:11
  • Does this answer your question? Angular - Dynamic CSS Class in iteration Commented Jan 13, 2020 at 9:12
  • 2
    according to the data provided you have a property called name but you are trying to access a undefined property called filename Commented Jan 13, 2020 at 9:12

4 Answers 4

2

Try like below,

Considering your object has name (i.e you mentioned it as filename but object has name property) and id properties. We can generate class like below

<div *ngFor="let item of arrayItems" [className]="item.name + item.id">
  ...
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

I am using @angular-devkit/architect 0.1202.14 @angular-devkit/build-angular 12.2.14 @angular-devkit/core 12.2.14 @angular-devkit/schematics 12.2.14 @angular/cdk 12.2.13 @angular/cli 12.2.14 @schematics/angular 12.2.14 rxjs 6.6.7 typescript 4.3.5 and it works! Can you verify it works and mark it as an answer ?
0

You can use String Interpolation:

class="{{obj.filename + obj.id}}"

Comments

-1

What about this ?

<div *ngFor="let item of items" [class]="item.name + item.id"></div>

Comments

-1

Change what you tried

[ngClass] = "{obj.name + obj.id}"

to this one

[ngClass] = "obj.name + obj.id"

I assumed obj.filename is typo of obj.name.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.