-2

I would like to apply a class with col-md-9 if displaysidebar is true else class should be col-md-12

This is what i have tried but fails to work

<div class=" displaysidebar ? col-md-9 : col-md-12 ">

Where am i going wrong?

2

3 Answers 3

4

You need to use NgClass along with property binding.

<div [ngClass]="displaysidebar ? 'col-md-9' : 'col-md-12'">
Sign up to request clarification or add additional context in comments.

Comments

1

Can do in either way:

<div [ngClass]="{'col-md-9': displaysidebar, 'col-md-12': !displaysidebar} ">

or

<div [ngClass]="displaysidebar? 'col-md-9': 'col-md-12';">

Comments

0

It should be something like

Template

 <div [ngClass]="(displaysidebar) ? 'col-md-9': 'col-md-12' ">

Component

   export class App {
      displaysidebar : boolean;
      constructor() {
        this.displaysidebar = true;
       }   

    }

3 Comments

dv is appreciated @jota haha, great way to support community now i guess
Rahul you are assigning string value to a boolean variable.
@Faisal thanks mate a typo my bad , had a spat with some one here thanks mate , i was editing it might have missed it . first it was like sidebar ===" true" . i changed to boolean missed that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.