I have found similar scenarios to this one, but I haven't yet found this exact scenario. Sorry if I missed it.
In Angular 8, I am using an *ngFor to iterate through some message objects. This displays the message title and date/time in a row of vertical boxes to the left of the screen. What I'm trying to get to is when one box is clicked, it becomes active and changes color. I have this working, but the problem is that the "active" class does not inactivate when another box is clicked. This is the problem I'm trying to solve.
Here is my current code with CSS:
<div *ngFor="let msg of messages; let i = index">
<a class="list-group-item list-group-item-action"
data-toggle="list" href="#home" role="tab"
[ngClass]="{ 'active' : isActive}" (click)="popIt(i)">
<span id="msgSubject1">{{msg.Subject}}</span>
<br />
<span class="datetimeCls" id="datetime1">
{{msg.DateCreated | date : 'short' }}
</span>
</a>
</div>
popIt(num: string) {
var number = num;
this.SubjectDisplay = this.messages[num].Subject;
this.DateDisplay = this.messages[num].DateCreated;
this.TextDisplay = this.messages[num].Body;
}
.list-group .list-group-item {
color: grey;
font-weight: bold;
font-family:"Gill Sans" Verdana;
font-size: 15px;
background-color: #FFFFFF;
}
.list-group .list-group-item.active {
background-color: lightgray !important;
border-top: solid 2px grey;
border-bottom: solid 2px grey;
color: grey;
}
This displays the object properties as expected, the clicks work, but it makes every box active as they're clicked. They do not inactivate.
Any direction, suggestions or examples would be appreciated! Thank you!
isActiveseems to be a common variable, you need to have something likemsg.isActiveand toggle the same inpopIt()Passmsgdirectly:popIt(msg)