2017-10-19 60 views
0

// HTML離子如何修復「ExpressionChangedAfterItHasBeenCheckedError」的錯誤?

<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:它檢查後表達發生了變化。上一個值:'今天5:34 PM'。當前值: ''。

我怎麼能解決這個錯誤?請幫忙。

+0

從模板刪除與副作用表達 – yurzui

+0

感謝答覆。你想告訴更多細節嗎? – fastworker399

+0

Angular在開發模式下運行兩個更改檢測週期。在第一次檢查你改變'this.lastdata'所以第二次檢查拋出錯誤 – yurzui

回答

0

難道你試試這個

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

謝謝回答。但沒有變化。 – fastworker399

0

如果像角,試着用

this._changeDetectionRef.detectChanges(); 

在你的方法結束時,不忘補充

private _changeDetectionRef : ChangeDetectorRef 

作爲擁有你的方法的Component構造函數的參數。

見discution here