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 remove the class only if the object's value is null

See original GitHub issue

I’m submitting a…


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

I have an icon component:

@Component({
  selector: 'icon',
  styleUrls: ['./icon.component.css'],
  template: `
    < span 
      [ngClass]="{'large': large, 'small': small}"
      class="mdi {{'mdi-'+name}}"
    >< /span>
  `
})
export class IconComponent  {
  @Input() name: string;
  @Input() large: boolean = false;
  @Input() small: boolean = false;
}

And the usage of it:

< icon [name]="show ? 'eye' : 'eye-off'"
       [small]="true"
       [large]="false">< /icon>

After the initial render the icon component’s < span> element will have only one class, the ‘small’. It’s okay. But after it I modify the name input’s value, the < span> element will have the class ‘large’ and ‘small’ both.

Expected behavior

After the modification of the name input’s value, the < span> element will have only the class ‘small’.

Minimal reproduction of the problem with instructions

https://stackblitz.com/edit/angular-vsffrd?file=app%2Fapp.component.html

Just click to the ‘switch the icon’ button. After it, the icon (< span> element) will be large, not small.

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

The documentation says, that the ngClass directive works with ‘false’ object value, but I experienced, it works only with value ‘null’. If I modify the large input’s value to ‘null’, it will work fine, but with ‘false’, it does not work.

This works only:

< icon [name]="show ? 'eye' : 'eye-off'"
       [small]="true"
       [large]="null">< /icon>

Environment


Angular version: >= 4.0.0-beta.3

Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [x] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX
 
For Tooling issues:
- Node version: v9.4.0
- Platform:  Linux

Others:
-

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:11 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
trotylcommented, Jan 31, 2018
1reaction
pkozlowski-opensourcecommented, Jan 31, 2018

#21937 should take care of this specific case.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Angularjs ng-class not removing old values when property ...
I have an angular app that is using an array to populate css rule sets in the view using ng-class. When the array...
Read more >
Angular NgClass Example – How to Add Conditional CSS ...
ngClass is a directive in Angular that adds and removes CSS classes on an HTML element. In this article, we are talking about...
Read more >
Angular ngclass: A Complete Guide - KnowledgeHut
Angular ngClass directive allows you to add and delete CSS classes from HTML ... Only if the value holds truth will the class...
Read more >
NgClass - Angular
... Object - keys are CSS classes that get added when the expression given in the value evaluates to a truthy value, otherwise...
Read more >
How do I use the Angular ngClass attribute - Kevin Chisholm
We know that adding and removing CSS classes is a common ... you may want to apply some CSS to an element, based...
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