Skip to content

Class binding is not working with custom key names #35851

Closed

Description

@bampakoa

🐞 bug report

Affected Package

The issue is caused by package @angular/common

Is this a regression?

No

Description

When I am trying to use class binding on an element with multiple classes and each key has more than one class names (e.g. classA classB), I get an error.

🔬 Minimal Reproduction

https://github.com/bampakoa/repro-app

Reproduction steps:

  1. Open the Angular application and run ng serve to run it
  2. Open the file app.component.ts
  3. Change the ngClass directive of the h1 element to class binding [class]
  4. Inspect the console of the browser for errors

🔥 Exception or Error

core.js:5873 ERROR DOMException: Failed to execute 'add' on 'DOMTokenList': The token provided ('classA classB') contains HTML space characters, which are not valid in tokens.
    at DefaultDomRenderer2.addClass (http://localhost:4200/vendor.js:51138:39)
    at applyStyling (http://localhost:4200/vendor.js:20043:49)
    at updateStyling (http://localhost:4200/vendor.js:28890:9)
    at updateStylingMap (http://localhost:4200/vendor.js:28841:13)
    at checkStylingMap (http://localhost:4200/vendor.js:28370:13)
    at Module.ɵɵclassMap (http://localhost:4200/vendor.js:28260:5)
    at AppComponent_Template (http://localhost:4200/main.js:119:65)
    at executeTemplate (http://localhost:4200/vendor.js:17342:9)
    at refreshView (http://localhost:4200/vendor.js:17189:13)
    at refreshComponent (http://localhost:4200/vendor.js:18629:9)

🌍 Your Environment

Angular Version:

Angular CLI: 9.0.4
Node: 12.16.0
OS: win32 x64

Angular: 9.0.4
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router
Ivy Workspace: Yes

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.900.4
@angular-devkit/build-angular     0.900.4
@angular-devkit/build-optimizer   0.900.4
@angular-devkit/build-webpack     0.900.4
@angular-devkit/core              9.0.4
@angular-devkit/schematics        9.0.4
@ngtools/webpack                  9.0.4
@schematics/angular               9.0.4
@schematics/update                0.900.4
rxjs                              6.5.4
typescript                        3.7.5
webpack                           4.41.2


Anything else relevant?

Activity

  1. pkozlowski-opensource commented on Mar 4, 2020

    @pkozlowski-opensource
    Member

    Live reproduction: https://ng-run.com/edit/TBihA8CFw9O70KgPtnLQ

    @bampakoa currently there are number of gaps in the functionality between [ngClass] and [class] bindings. In the long term we want to unify those but we are definitively not there yet. As such you can't at this point, expect that both will be equivalent.

    Is there any particular reason that you would prefer to using [class] vs. [ngClass]?

  2. added
    area: core
    on Mar 4, 2020
  3. added this to the needsTriage milestone on Mar 4, 2020
  4. added
    feature
    on Mar 4, 2020
  5. modified the milestones: needsTriage, Backlog on Mar 4, 2020
  6. bampakoa commented on Mar 4, 2020

    @bampakoa
    ContributorAuthor

    @pkozlowski-opensource no, not anything particular.I read in the docs that

    The NgClass directive can be used as an alternative to direct [class] bindings. However, using the above class binding syntax without NgClass is preferred because due to improvements in class binding in Angular, NgClass no longer provides significant value, and might eventually be removed in the future.

    I think that the documentation at this point should be improved for the corner case that I mentioned.

  7. jelbourn commented on Jan 28, 2021

    @jelbourn
    Contributor

    Closing this since, for the time being, we don't plan to bridge the feature differences between NgClass and [class], but we should definitely document the gap, tracked in #40623

  8. angular-automatic-lock-bot commented on Feb 28, 2021

    @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.

  9. locked and limited conversation to collaborators on Feb 28, 2021
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: corecore: styling bindingsfeaturefreq1: low

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions