In Angular you can use ngClass to conditionally apply your class like so.
<div
*ngFor="let group of step.groups; let firstItem = first;"
class="layout--stepper__subgroup"
ngClass]="{'is-active': group.prop === activeProp}"
(click)="scrollToSection(group)">
p class="t-data">{{group.header}}</p>
</div>
scrollToSection(group) {
this.activeProp = group.prop;
let scroll = document.querySelector(`#${group.prop}`).scrollIntoView();
}
Now whenever I click the item in my list the class is-active will be applied to that specific list item. However, what if I want to apply is-active to the first item on the list by default?
If I change my code to be:
ngClass]="{'is-active': group.prop === activeProp || firstItem}"
Now both the first item on the list and the item clicked will both have the is-active class. How can I set the first item on the list to have the is-active class by default and then remove it when I click another item on the list?
ngClass]="{'is-active': group.prop === activeProp || (firstItem && activeProp===undefined )}"should work. - Jojofoulk