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 and [class] works bad together

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

I’m trying to use both ngClass and [class] to set classes that a need like here <h1 [ngClass]="{'ui-true-class': true, 'ui-false-class': false}" [class]="styleClass">Hello</h1> <button (click)="styleClass='styleClassValue'">set styleClass</button>

Current behavior

When some value setted to styleClass variable it also writes all key-classes from ngClass’es map that has false value(ui-false-class in example) Expected behavior

Setting value to styleClass doesn’t affect classes from ngClass value like described above Minimal reproduction of the problem with instructions you can see it here plunk

  1. see initial value class on element “Hello” is “ui-true-class”
  2. click “set styleClass” button
  3. see value class on element “Hello” is “ui-true-class styleClassValue ui-false-class”

I’ve tried to look deeper and found that after changing styleClass value like in example classes from ngClass setted after this sequence of method calls: set ng_class.js NgClass._applyClasses(this._rawClass, false) ng_class.js and code here Object.keys(rawClassVal).forEach(function (klass) { if (isPresent(rawClassVal[klass])) _this._toggleClass(klass, !isCleanup); });

So rawClassVal[klass] by itself doesnt matter here and klass always will toggle since second parameter in NgClass._applyClasses(which called isCleanup) equals ‘false’.

What is the motivation / use case for changing the behavior?

Please tell us about your environment:

  • Angular version: 2.0.X
  • Browser: [Chrome | Firefox ] at least
  • Language: [all | TypeScript X.X | ES6/7 | ES5]

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vytautas-pranskunas-commented, Apr 12, 2017

I noticed same behavior with interpolation like: <div class="some-class-{{someVariable}}" [class.new-class]="true">

new-class is not added

0reactions
angular-automatic-lock-bot[bot]commented, Sep 13, 2019

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Difference between [ngClass] vs [class] binding - Stack Overflow
ngClass is more powerful. It allows you to bind a string of classes, an array of strings, or an object like in your...
Read more >
Angular Classes with NgClass - Ultimate Courses
In this post you'll learn how to use the NgClass directive in Angular to dynamically assign class names to elements or components.
Read more >
NgClass - Angular
Array - the CSS classes declared as Array elements are added,; Object - keys are CSS classes that get added when the expression...
Read more >
Angular ng-class Directive - W3Schools
As an object, it should contain key-value pairs, where the key is the class name of the class you want to add, and...
Read more >
Angular ngclass: A Complete Guide - KnowledgeHut
Angular ngClass directive allows you to add and delete CSS classes from HTML elements. Classes apply a group of style properties to all...
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