-1

I am trying to apply multiple (in my case 2) classes onto a custom component which contains a list item component from MDBootstrap:

App.module.ts

<custom-list-component size="w-50">
    <custom-list-item-component href="#">list item 1</custom-list-item-component>
    <custom-list-item-component href="#">list item 2</custom-list-item-component>
</custom-list-component>

Custom-list-component.component.html

<div [ngClass]="size" class="list-group">
  <ng-content></ng-content>
</div>

Custom-list-component.component.ts

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

@Component({
  selector: 'custom-list-component',
  templateUrl: './custom-list-component.component.html',
  styleUrls: ['./custom-list-component.component.sass']
})
export class CustomListComponentComponent implements OnInit {

  @Input() size: string;
  testHorizontal: string = "list-group-horizontal";

  constructor() { }

  ngOnInit() {

  }
}

When I "just" apply size like I did in the custom list component HTML, it applies w-50 to the list, which causes it to display on 50% of the page:

enter image description here

But now, I want to apply a second "class", I want to apply list-group-horizontal to the div that had w-50 applied to it. I have been looking in the docs and I tried all of the below solutions:

<some-element [ngClass]="'first second'">...</some-element>

<some-element [ngClass]="['first', 'second']">...</some-element>

<some-element [ngClass]="{'first': true, 'second': true, 'third': false}">...</some-element>

<some-element [ngClass]="stringExp|arrayExp|objExp">...</some-element>

<some-element [ngClass]="{'class1 class2 class3' : true}">...</some-element>

For example, this does not work:

<div [ngClass]="'size testHorizontal'" class="list-group">
  <ng-content></ng-content>
</div>

Because w-50 and list-group-horizontal are not applied to the div, only their names. This also applies to the other options above. It looks like this:

enter image description here

How would I apply the content of the properties to the DIV and not their names?

Thanks in advance.

Paras Korat
  • 2,011
  • 2
  • 18
  • 36
Niek Jonkman
  • 1,014
  • 2
  • 13
  • 31

1 Answers1

1

ngClass directive needs a string to appends as a class (in your case).

so we have to make a string with our variables.

[ngClass]="size +' '+ testHorizontal"

constructs a string with two variables size +' '+ testHorizontal

Sheik Althaf
  • 1,595
  • 10
  • 16