TomP TomP - 1 year ago 132
Javascript Question

Angular 4 ngFor set class for element

I am new to Angular and I am trying to make side menu using material lists and ngFor.

*ngFor="let linkItem of linkItems"
</md-list-item >

everything works fine except setting
. IS there any different approach to do it?


import { Component, OnInit } from '@angular/core';

import { LinkItem } from './link-item';

const LINK_ITEMS: LinkItem[] = [
linkName: 'Weather',
className: 'list-item',
routerLink: '/weather'
linkName: 'Top visits',
className: 'list-item',
routerLink: '/visits'
linkName: 'Photo Gallery',
className: 'list-item',
routerLink: '/gallery'

selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']

export class AppComponent implements OnInit {
title = 'My forum';

linkItems: LinkItem[] = LINK_ITEMS;
selectedListItem: LinkItem;

constructor() {

ngOnInit(): void {
this.selectedListItem = null;

listItemClickToggle(linkItem: LinkItem): void {

Answer Source

You should use [ngClass] instead of just class

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download