Error: NgClass on mat-tab-group caused an ExpressionChangedAfterItHasBeenCheckedError due to a change in the expression after it was checked

Trying to create a scenario where the mat-ink-bar in my mat-tab-group has different colors. Using local references and NgClass. Even though the styles are working fine, getting an error in the console:

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'one'. Current value: 'two'.

Here's the code snippet:
HTML:

<mat-tab-group
  class="<some other classes>"
  ...
  #tabGroup
  [ngClass]="tabGroup.selectedIndex === 1 ? 'one' : 'two'"
>

SCSS:

.one.mat-primary .mat-ink-bar{
  background: blue !important;
}

.two.mat-primary .mat-ink-bar{
  background: red !important;
}

Reading up on NgClass posts, noticing that true and false values keep changing. Wondering why I'm experiencing this error using a different approach. Could it be due to other developers modifying variables within NgAfterViewChecked or NgOnChanges?
Appreciate any insights!

Answer №1

One issue arises because the value of selectedIndex can change within the tab group after the check on [ngClass]. To resolve this, you should listen for (selectedTabChange) and manually keep track of the selected index.

Here is a concise code example:

<mat-tab-group 
  `(selectedTabChange)`="selectedTab = $event.index" 
  [ngClass]="selectedTab === 1 ? 'one' : 'two'"
>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Developing microfrontends using Angular involves loading and navigating a compiled package from npm

I have been struggling to find a solution to an ongoing issue and wasting time on futile attempts. Currently, I am working with Angular 15 within a microfrontend architecture. My goal is to implement a system where I can download a compiled microfrontend ...

Angular Markdown Styling: A Guide

Currently, I am using Angular and Scully. I would like to add style to the image in a markdown file within Angular, but I am unsure of how to accomplish this task. The image is currently displaying too large. Can anyone provide me with useful advice on how ...

How can I repeatedly trigger an eventListener in JavaScript?

I'm currently facing an issue with calling the event listener for all 4 progress bars on my page. The problem is that it's only working on the first progress bar. I cloned the div with the id of 'mycontainer' using a for loop, but the e ...

Encountering a JSON error while attempting to login through an API on the Ionic platform

While implementing the login functionality through API in Ionic, I encountered the following error: Error: Property 'json' does not exist on type '{}'. Below is my loginpage.html code: <ion-header> <ion-navbar> & ...

Customizing the appearance of an HTML element using user-input text styling

On my webpage, there are two text areas and a division. One of the text areas is meant for users to input HTML code, while the other is for CSS code. Whenever a button is clicked, a JavaScript function will be triggered to display the combined HTML and CSS ...

Anticipating the resolution of promises and observables in Angular 2

Within my accountService module, there is a dialog prompt that requests the user's username and password, returning a promise. If the user clicks on close instead of dismissing the dialog box and the validators require the input data before allowing t ...

What is the best way to make just the background image transparent using HTML and CSS?

Hey there! I'm trying to make just the background image transparent, while leaving everything else non-transparent. Is this possible with Bootstrap? Here's a snippet of my HTML: HTML Section: <!-- Homepage Section --> <section id= ...

Tips on ensuring iframe is consistently centered

I have a div element enclosing an iframe, and I am trying to always position it in the center. Here is my attempt on jsfiddle: jsfiddle I truly believe that someone could help because I am certain that it is something simple but I just can't figure i ...

When transitioning to angular 10 and removing decorators from classes, what is the best approach for dealing with a base class that is inherited by both Directives and Injectables?

During the angular 10 migration process, there is a recommendation that classes utilizing angular features should have a decorator. However, what should be done in cases where it's a base class shared by both Injectables and Directives (as it solely i ...

The ReactBootstrap flexbox feature does not automatically adjust its height to match the content within

My struggle is with getting the height of this flexbox column to match that of its content. When the window width is less than 576 pixels, only one column is displayed per row. However, in this scenario, one of the columns (the teal one) ends up being too ...

In Angular 4 service, the requested resource does not have the 'Access-Control-Allow-Origin' header

I am currently utilizing a service that calls a URL where an image file is located. However, when the URL is called, an error appears in the console stating: Failed to load "my file url": No 'Access-Control-Allow-Origin' header is present on the ...

Angular: Discover the best way to delegate translation tasks to your S3 bucket!

I am currently using ngx-translate for handling translations in my Angular application. Everything is functioning properly when the translation files are stored within the app itself. However, I want to move all of my JSON translation files to an AWS S3 bu ...

Ways to eliminate color from a link upon clicking a different one

I had a specific idea in mind - to create a simple menu bar where clicking on a particular link would display related content below. The chosen link should turn blue when selected. I managed to come up with the following code: <a href="#" class="link"& ...

The text is placed below the image, rather than appearing alongside it

My website bby is supposed to be displayed right next to the image logo, but it's showing up under the logo for some reason. Can someone help me with this issue? You can view the current layout here This is the html code I have: body { font-si ...

What is the best way to update a targeted component in React when triggered by an event handler?

Your goal may seem straightforward, but getting a reference to a specific component using this is proving to be tricky. Here we have our App.js file: import React, { Component } from 'react'; import CoolBox from './coolBox.js'; import ...

A guide on using jQuery to include an icon within a select option

I'm having trouble adding an icon to a select dropdown using jQuery. I attempted to include the icon within the option itself, but it displays as empty boxes. Additionally, I want the selected option's icon to appear above without the accompanyin ...

Issue with Nivo Lightbox: image not properly centered or resizing correctly

Whenever an image is clicked, it opens in the Nivo lightbox. This feature used to work flawlessly, but recently, it seems to be experiencing issues. The image should be centered and resize responsively to stay in the center. (For example, it worked correct ...

Tips for creating a stylish, blurred, and centered box for a login form on a full-size background that is also responsive

I attempted to create a login form on an HTML page using Angular, featuring a full-size background image that is centered. The form itself is contained within a div with a blurred background, also centered both horizontally and vertically within the browse ...

In order for Firefox to properly recognize and apply the @font_face, it must be located in the root

I'm working on a website that has a custom font, and I've implemented the @font-face code as shown below: @font-face { font-family: 'webfontregular'; src: url('fonts/cracked-webfont.eot'); src: url('fonts/cra ...

Adding Multiple CSS Classes to an HTML Element using jQuery's addClass Feature

Take a look at this HTML code snippet: <html> <head> <style type="text/css"> tr.Class1 { background-color: Blue; } .Class2 { background-color: Red; } </style> </head> <body> < ...