How to change class of individual elements inside ngfor?

Charles Webb

I'm very sorry if this question has been answered however I was unable to find an appropriate answer. I am attempting to create a simple chat application and wish for messages sent by the current user to appear on the right hand side of the screen whilst messages from other users will appear on the left.

I believe I am most of the way there with this however its not functioning properly, if the most recent message is sent by the current user then all the message will be on the right vica versa for messages from other users.

My question in more detail therefor is, is there a way for me to change the class for a list item that is generated by ngFor, with each element possibly having a different class to any other list item.

Below is the HTML:

    <ion-item *ngFor="let message of messages; let i = index" (click)="beingWhisper(message)">

    <ion-card [ngClass]="styleClass">
      <ion-card-header class="messageHeader">
        {{i}}{{message.Person}}
      </ion-card-header>
      <ion-card-content>
        {{message.Message}}
      </ion-card-content>
    </ion-card>

</ion-item>

Here is the very basic CSS:

.message-other{
  float: left;
  width: 50%;
}
.message-self{
  float: right;
  width: 50%;
}

The ngClass variable is controlled simply with this variable on my component class, this is changed to be either "message-self" or "message-other" when required

    styleClass = '';

Fixed: In case anyone finds this question with a similar problem, my issue was fixed by using the individual messages property, which held the senders ID and if it was the same as the current user ID then apply the style I wanted, this was discovered through dfsq's suggestion, with thanks to Günter Zöchbauer as his suggestion was also correct

dfsq

What you should do in cases like this is to compare currently authenticated used id with message id, which should be a sender used id. Then you ngClass look something like this:

[ngClass]="message.userId === currentUser.id ? 'message-self' : 'message-other'"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change the font and colour of individual array elements

How to change the font and colour of individual array elements

How to bind individual elements of a vue array to a class

Change class style (not individual elements) that's not in stylesheet using Javascript

How to add class to Angular's ngFor middle elements, or a default class

Cannot Change Individual Elements in an Array

how to change the image url based on color click in angular inside ngFor

How to display complex data or to change template for the first and last elements in ngFor?

How to add class(es) to elements generated by ngFor on hover?

How to set a class only to specific elements when using *ngFor?

How to change all elements inside array?

How can I change elements inside and array

how to change order of html elements inside a div

Change element inside ngFor with a ngClick

How to change values of an array inside a class

How to change static variable inside in a class by a method

How to change instance variables inside anonymous class

javascript singleton how to change properties inside a class

How to i change the class name inside <td>

how to access the individual elements of a python <class 'requests.models.Response'> class

How to change the class of elements that have matching content

How to display the individual elements of two identical columns inside a 2D array in C

How to lock individual elements in JointJS?

angular viewChild for dynamic elements inside ngFor

Add class to individual elements when in viewport

Toggling individual elements of the same class in dropdown menu

Toggle Class for individual elements in Vue JS

Removing duplicate elements of a vector of pairs based on individual elements inside the pair

How can I toggle individual <li> elements using only basic javascript and DOM manipulation to change their styling?