Repository navigation
Description
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
- see initial value class on element "Hello" is "ui-true-class"
- click "set styleClass" button
- 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
pkozlowski-opensource commentedon Mar 22, 2017
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 usingngClassexclusivelyvytautas-pranskunas- commentedon Apr 12, 2017
I noticed same behavior with interpolation like:
<div class="some-class-{{someVariable}}" [class.new-class]="true">new-class is not added
vicb commentedon Nov 30, 2017
As @pkozlowski-opensource said, you should not use them at the same time.
artuska commentedon Jun 27, 2018
+1
Have the same problem with class interpolation + ngClass:
When the
typechanges button getsprocessingclass, wtf?isProcessingis always set tofalse!pkozlowski-opensource commentedon Jun 27, 2018
@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.
angular-automatic-lock-bot commentedon 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.