0

I have a common service and I am using my service to change a variable value using click function between components. But I an getting and undefined error and also

ERROR
Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngSwitch: count1'. Current value: 'ngSwitch: undefined'.

I have a made the stackblitz demo (https://stackblitz.com/edit/angular-8to3ra?embed=1&file=src/app/app.component.html) here. It would be a great help if some body just point me out what I am doing wrong. As I am very new to Angular any help would be greatly appreciated. My first question here is why I am getting undefined in my parent component from service?My second question it is not updating the count value in my parent component?

  • What information have you found when you have tried to research this problem? – Kurt Hamilton Mar 02 '20 at 07:06
  • may be this is useful for you: https://stackoverflow.com/a/39787056/3326975 – Abu Sufian Mar 02 '20 at 07:08
  • ChangeDetectorRef only show the last updated value. I want to update the count value on each click. –  Mar 02 '20 at 07:11
  • "It would be a great help if some body just point me out what I am doing wrong" You are expected to put a little effort into researching your problem before you post your problem. – Kurt Hamilton Mar 02 '20 at 07:14
  • Who told you I am not putting any effort here? –  Mar 02 '20 at 07:16
  • May be in code `*ngSwitchCase="getPrev()"` you calling this method as a case but this method `getPrev(count: string)` expect one param in component and you getting `undefined` you count at `this.count = this.CommonService.getPrev(count)`. – hrdkisback Mar 02 '20 at 07:26
  • @hrdkisback could you please suggest how to solve this problem? Where do I need to modify my code? –  Mar 02 '20 at 07:27
  • @PiyaliGhosh Please refer the Eliseo's answer. – hrdkisback Mar 02 '20 at 07:48
  • @hrdkisback I already did that. –  Mar 02 '20 at 07:49
  • @PiyaliGhosh Check this https://stackblitz.com/edit/angular-x5tymd?file=src/app/your-data/your-data.component.html – hrdkisback Mar 02 '20 at 09:29
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/208846/discussion-between-piyali-ghosh-and-hrdkisback). –  Mar 02 '20 at 12:59

1 Answers1

1

you has anything wrong in your 'your-data.component.html'. It is not ?

<div [ngSwitch]="count"> <!--you wrote [ngSwitch]=count -->
        <app-census (next)="onNext($event)" *ngSwitchCase="'count1'"></app-census>
        <!--you wrote <app-application ... *ngSwitchCase="getPrev()">-->
        <app-application (next)="onNext($event)" *ngSwitchCase="'count2'">
        </app-application>        
</div>
Eliseo
  • 50,109
  • 4
  • 29
  • 67
  • Thanks for the answer. Now I am getting the value in my service. But it is not updating in your-data.compone.ts –  Mar 02 '20 at 07:48