question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NgClass conditional formatting with multiple classnames ommits first class in the first condition

See original GitHub issue

I’m submitting a … (check one with “x”)

[x] bug report => search github for a similar issue or PR before submitting
[ ] feature request
[ ] support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior Using NgClass for conditional formatting based on a boolean, results in actual applied class that is missing the one element on the first condition (if the first condition is true).

Expected behavior The full class value should be presented in actual html on all cases.

Minimal reproduction of the problem with instructions plnkr: http://plnkr.co/edit/4qw7oFfEchLPmYIssHA1

Template code: <div [ngClass]=“{‘green bold’: true, ‘red bold’: false}”>first</div> <div [ngClass]=“{‘green bold’: false, ‘red italic’: true}”>second</div>

First div should get green and bold classes. It only gets green. Second div should get red and italic classes and gets them both.

Actual:

div ng-reflect-ng-class=“[object Object]” class=“green”>first</div> div ng-reflect-ng-class=“[object Object]” class=“red italic”>second</div>

What is the motivation / use case for changing the behavior? It seems that this is a bug. In my specific code it styles table TD’s according to a boolean value in the underlying object.

Please tell us about your environment: Windows 7 / Centos 6.5, node 6.9.1, ng2 2.2.1

  • Angular version: 2.2.1

  • Browser: [all]

  • Language: [TypeScript 2.0.3]

  • Node (for AoT issues): node --version = 6.9.1

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

17reactions
DzmitryShylovichcommented, Nov 27, 2016
<div [ngClass]="{'green bold': true, 'red bold': false}">

actually u are saying that u want bold to be true and false at the same time which doesn’t make much sense

 <div class="bold" [ngClass]="{'green': true, 'red': false}">

is the correct usage

1reaction
leiradkcommented, Apr 5, 2018

`from HTML <i [ngClass]=“ngClassForIcons( name )”> Sample here

From components ngClassForIcons(value){ let data = ‘’; if(value == ‘Gerald’){ data = ‘Great Class’; }else if(value == ‘Ariel’){ data = 'Extreme Class; }else{ data = ‘Another Unknown’; } return data; }`

this is for better Idea if your class has a space because sometimes if you use the ngClass expression some won’t work for your desired output Thank you for reading hope will help you.``

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular: conditional class with *ngClass - Stack Overflow
Angular version 2+ provides several ways to add classes conditionally: type one [class.my_class] = "step === 'step1'". type two [ngClass]="{'my_class': step ...
Read more >
How to add conditional class in Angular using ngClass
We can add a single class or multiple classes dynamically based upon the conditions. We will go through few examples to understand it...
Read more >
ng class multiple classes same condition Code Example
Answers related to “ng class multiple classes same condition”. ng class conditional angular. Queries related to “ng class multiple classes same condition”.
Read more >
Angular | Ng-Class Conditional Expressions , If Else for ...
This directive can be used to add/ remove classes by applying single or multiple conditions at the same time in a single [ngClass]...
Read more >
disable and enable a button based on condition react js
How to disable a button in JavaScript based on condition Example. In the example, we have 2 input fields first for text and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found