2

I am building a page that has tabs - those tabs are dynamics from the DB but to make it simple I defined it manually.

Each tab has a the same radio buttons (there is also a save button and more but to make it simple - I removed it).

When I choose one of the radio buttons I am getting this error

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ui-state-active: false'. Current value: 'ui-state-active: true'.
at viewDebugError (core.js:9817)

Also after clicking it again there is no error

Basically the question is how to create radio buttons inside a dynamic tab.

I read this: https://blog.angular-university.io/angular-debugging/

and this: https://blog.angularindepth.com/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error-e3fd9ce7dbb4

but since I am new to Angular - I can't understand how to solve my problem.

Here is the full code

 <p-tabView> <p-tabPanel [header]="category" *ngFor="let category of categoriesList; let i = index" [selected]="i == 0"> <br> <div class="ui-grid-row"> <h3>Service experience for {{category}}</h3> </div> <div class="ui-grid-row"> <div class="ui-g-6"> <div class="ui-g-12"> <p-radioButton name="quickFeedBackGrade" value="1" label="Very poor" [(ngModel)]="categoryGrade" ></p-radioButton> </div> <div class="ui-g-12"> <p-radioButton name="quickFeedBackGrade" value="2" label="Poor" [(ngModel)]="categoryGrade" ></p-radioButton> </div> <div class="ui-g-12"> <p-radioButton name="quickFeedBackGrade" value="3" label="Good" [(ngModel)]="categoryGrade" ></p-radioButton> </div> <div class="ui-g-12"> <p-radioButton name="quickFeedBackGrade" value="4" label="Very good" [(ngModel)]="categoryGrade" ></p-radioButton> </div> <div class="ui-g-12"> <p-radioButton name="quickFeedBackGrade" value="5" label="Ecxelent" [(ngModel)]="categoryGrade" ></p-radioButton> </div> </div> </div> </p-tabPanel> </p-tabView> 

and the tab.ts holds only the tab building

import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-tabs', templateUrl: './tabs.component.html', styleUrls: ['./tabs.component.css'] }) export class TabsComponent implements OnInit { categoriesList: any = ['tab1' , 'tab2' , 'tab3' , 'tab4' ]; constructor() { } ngOnInit() { } } 
12
  • I don't think this is the relevant code. The value that it's complaining about is ui-state-active. Can you add the component with that property please? Commented Jul 29, 2018 at 8:34
  • sorry for some reason it was was displaying the tab part in the html i updated the question Commented Jul 29, 2018 at 8:59
  • Sorry, but I still can't see ui-state-active being used in your template... Commented Jul 29, 2018 at 9:01
  • i don't know where it gets it from - i also did not use it maybe from the primeNg <p-radioButton > Commented Jul 29, 2018 at 9:04
  • 1
    I suppose you are in debug (dev) mode. This bug (in fact it's a security checking) occurs when a function detects a change, and you change the changed element, that will fire a change event, leading to changing the changed element, etc. (onChange) = "displayFeedback($event)" is probably the source of the problem. Commented Jul 29, 2018 at 9:04

1 Answer 1

2

It looks like you'll need to create a categoryGrade for each tab.

Change it to an array, like so: categoryGrade = [];, then change your ngModel so that it becomes [(ngModel)]="categoryGrade[i]".

This way, each tab will have it's own value for categoryGrade, and the problem will no longer occur.

Here is a working StackBlitz

Sign up to request clarification or add additional context in comments.

4 Comments

thank you I understand my mistake I don't see this error now . but still when switching from tab to tab i don't see the previus pick - any idea what causing it ? once it is stored in the categoryGrade[i] it should also display it
What do you mean? As it stands, the checkboxes on each tab will be independent. Do the tabs need to share the same values? I'm not sure why they're in tabs if each tab is going to be the same
i mean that if you are in tab 1 and choose "poor" then go to tab 2 and choose "very good" and then go back to tab1 - you need to see the value "poor" that you chose before
Sorry, you'll also need to remove the name attribute (or find a way of using a unique name for each tab). I've updated the StackBlitz with the name removed

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.