fastworker399
fastworker399

Reputation: 423

Ionic how to fix "ExpressionChangedAfterItHasBeenCheckedError" error?

//html

    <span style="margin-left:43%;background-color:rgb(229,229,229);border-
    radius:10%">&nbsp;&nbsp;{{formatEpoch(epoch)}}&nbsp;&nbsp;</span>

//ts

lastdate:any;                    

 formatEpoch(epoch): string {
    if(epoch == this.lastdate){
        return '';
    }else{ 
    this.lastdate =epoch;
    return UtilService.getCalendarDay(epoch);
    }
  }

ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '今天 5:34 PM'. Current value: ''.

How can i fix this error? Please help.

Upvotes: 0

Views: 1911

Answers (3)

the best solution:

  import { Platform } from '@ionic/angular';

  // ...

  constructor(
    ...
    private _platform:Platform
  ) { }

  ngOnInit() {
    this._platform.ready().then(() => {
      this.formatEpoch()
    })
  }

Upvotes: 0

user1767316
user1767316

Reputation: 3631

if like in angular, try to use

this._changeDetectionRef.detectChanges();

at the end of your method, not forgetting to add

private _changeDetectionRef : ChangeDetectorRef

as parameter of the constructor of the Component owning your method.

See discution here

Upvotes: 1

Raja Mohamed
Raja Mohamed

Reputation: 1026

Could you try this

lastdate:any;                    
formatEpoch(epoch): string {
   setTimeout(()=> {
     if(epoch == this.lastdate){
        return '';
     }else{ 
       this.lastdate =epoch;
       return UtilService.getCalendarDay(epoch);
     }
   }, 100);
}

Upvotes: 0

Related Questions