Skip to content

ngClass and [class] works bad together #15396

Closed

Description

@sur-run

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 =

Activity

  1. pkozlowski-opensource commented on Mar 22, 2017

    @pkozlowski-opensource
    Member

    Using [class] and [ngClass] together is asking for trouble as you've got multiple bindings to the same property. I will have a look at this one to see if we can have sth reasonable here but I would suggest moving class calculation logic to a function and using ngClass exclusively

  2. vytautas-pranskunas- commented on Apr 12, 2017

    @vytautas-pranskunas-

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

    new-class is not added

  3. vicb commented on Nov 30, 2017

    @vicb
    Contributor

    As @pkozlowski-opensource said, you should not use them at the same time.

  4. artuska commented on Jun 27, 2018

    @artuska

    +1

    Have the same problem with class interpolation + ngClass:

    <button class="button button--medium button--special button--special--{{type}}" [ngClass]="{'processing': isProcessing}">
        <span class="button__text button__text--medium">
            <span>Подключить</span>
        </span>
    </button>
    

    When the type changes button gets processing class, wtf? isProcessing is always set to false!

  5. pkozlowski-opensource commented on Jun 27, 2018

    @pkozlowski-opensource
    Member

    @artuska it should have been fixed from Angular 6.0.0-rc.2 via 4a42669.

    Please open a new issue with a minimal reproduce scenario (plunker / stackblitz) if you still face problems with Angular 6.

  6. angular-automatic-lock-bot commented on Sep 13, 2019

    @angular-automatic-lock-bot

    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.

  7. locked and limited conversation to collaborators on Sep 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: commonIssues related to APIs in the @angular/common packagetype: bug/fix

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions