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.

Responsive ngClass (ngClass.xs...ngClass.xl) removing class applied by normal ngClass

See original GitHub issue

Bug Report

When we have Responsive ngClass and normal ngClass responsive ngClass removed class applied by normal ngClass

Example:

<div ngClass="box" ngClass.xs="extra-small-device" ngClass.sm="small-device" ngClass.md="medium-device" ngClass.lg="large-device"
 ngClass.xl="extra-large-device">
	test
</div>

What is the expected behavior?

For above code that div should always have class “box”. Also based on screen size that responsive class also should get added

What is the current behavior?

Only responsive class get added that default class “box” got removed

What are the steps to reproduce?

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

What is the use-case or motivation for changing an existing behavior?

Which versions of Angular, Material, OS, TypeScript, browsers are affected?

{
    "@angular/cdk": "^8.0.0-rc.0",
    "@angular/common": "^8.0.0",
    "@angular/compiler": "^8.0.0",
    "@angular/core": "^8.0.0",
    "@angular/flex-layout": "^8.0.0-beta.26",
    "@angular/forms": "^8.0.0",
    "@angular/platform-browser": "^8.0.0",
    "@angular/platform-browser-dynamic": "^8.0.0",
    "@angular/router": "^8.0.0",
    "core-js": "2",
    "rxjs": "^6.5.2",
    "zone.js": "^0.9.1"
  }

Is there anything else we should know?

Issue Analytics

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

github_iconTop GitHub Comments

3reactions
fxckcommented, Jul 23, 2019

update from 7 to 8, now

      <div
        ngClass.xs="__top-content--xs"
        ngClass.sm="__top-content--sm"
        class="__top_content">

__top_content class is removed completely

2reactions
retripconcommented, Oct 20, 2019

Just stumbled across the same issue. I would expect the ngClass directive to be applied all the time. As @ravinsinghd figured out this behaviour is also described in the documentation.

However, the class directive did work for me and is not overwritten by the responsive ngClass and I can use this as workaround.

Does not work (but I would expect test to be applied except it is overwritten in the ngClass.lt-md): <div [ngClass]="{ 'test': true }" [ngClass.lt-md]="{ 'bla': true }"

Does work for me: <div [class.test]="true" [ngClass.lt-md]="{ 'bla': true }"

The workaround gets kind of messy and hard to read if you want to apply multiple classes with the regular ngClass.

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular/flex-layout How To: different class based on screensize?
Yes, there is a way! Just try this: <div [ngClass.xs]="'myNarrowClass'" [ngClass.md]="{'myWideClass': true}"></div>. Here are the docs.
Read more >
ngClass - AngularJS: API
The ngClass directive allows you to dynamically set CSS classes on an HTML element by databinding an expression that represents all classes to...
Read more >
NgClass - Angular
Adds and removes CSS classes on an HTML element. See more... Exported fromlink · CommonModule ...
Read more >
Angular NgClass Example – How to Add Conditional CSS ...
ngClass is a directive in Angular [https://angular.io/api/common/NgClass] that adds and removes CSS classes on an HTML element.
Read more >
From ng-class to ngClass - Telerik Blogs
The ng-class directive in Angular 1.x allows you to dynamically add CSS classes based on a configuration object. This is a special key-value ......
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