0

Im not quite sure something like this is possible but say in my html component I have an ngFor like so..

<div *ngFor="let card of cards">
   ... stuff in here
</div>

now say I have an array of classNames like so

classNames = [
   'red',
   'yellow',
   'blue',
   'green'
]

and inside my *ngFor I have a div like so

<div *ngFor="let card of cards">
   <div [class]='...'>
      <div class="card">
      </div>
   </div>
</div>

basically what I want to happen is for every item in the ngFor give loop through the classNames array and dynamically add it to the incoming data so for example

say I have 6 items in cards so each card needs a classname so it loops through classNames and gives it a class so like this..

<div [class]='red'>
   <div class="card">
   </div>
</div>
<div [class]='yellow'>
   <div class="card">
   </div>
</div>
<div [class]='blue'>
   <div class="card">
   </div>
</div>
<div [class]='green'>
   <div class="card">
   </div>
</div>
<div [class]='red'>
   <div class="card">
   </div>
</div>
<div [class]='yellow'>
   <div class="card">
   </div>
</div>

and so on and so forth..

how could i accomplish something like this?

EDIT

component.html

<div class="card" *ngFor="let card of cards; let i = index">
   <div [class]="classNames[i%classNames.length]">
      ....
   </div> 
</div>

component.ts

export class...
  classNames = [
    'light-green',
    'dark-green',
    'aqua',
    'blue',
    'blue-purple',
    'purple',
    'purple-pink',
    'purple-orange'
  ];
1
  • you have to intilaize the array in .ts code Commented May 10, 2018 at 5:06

2 Answers 2

2

You can leverage remainder (%) operator to achieve that:

<div *ngFor="let card of cards; let i = index">
   <div [class]="classNames[i%classNames.length]">
      <div class="card">
        {{ card }}
      </div>
   </div>
</div>

Ng-run Example

Update:

You should define array as follows:

classNames = [
   'light-green',
   'dark-green',
   'aqua',
   'blue',
   'blue-purple',
   'purple',
   'purple-pink',
   'purple-orange'
];

Note: i use = instead of :

Sign up to request clarification or add additional context in comments.

5 Comments

Hey Im getting this error Cannot read property 'length' of undefined any idea what could be wrong?
Did you check my example? I think you're doing something wrong and don't see what i wrote
Ive done it exactly the same as your example, not sure why it wouldnt be working..
Can you reproduce it?
Updated my answer
0

Instead of randomly applying any class to any card or deciding it on view based on some %, the best way, I believe would be read it from the Cards object itself, since it is logical to have all details of a card read from the card itself.

So that view is independent of those extra stuffs.

classNames = ['red','yellow','blue','green'];
cards = [{text: 1, class: this.classNames[0]},{text: 2, class: this.classNames[1]}];

your view should simply do its task (render)

<div *ngFor="let card of cards; let i = index">
  <div [class]="card.class">
    <div class="card">
      {{ card.text}}
    </div>
  </div>
</div>

2 Comments

The cards object comes from an api I have no control over, thus why Im doing it randomly, if I could put the classNames on the card itself I would
@JuvenileSnow that's where view-model come in picture

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.