简体   繁体   English

调用toISOString()会引发“ ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。”

[英]Calling toISOString() thows “ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked.”

I have this method here in a component: 我在组件中有此方法:

getTime(): Date {
  const date = new Date();
  date.setHours(this.hour);
  date.setMinutes(this.minute);
  date.setSeconds(0);
  return date;
}

which I call within another: 我称之为另一个:

<input matInput [hidden]="true" formControlName="time"
           [ngModel]="this.timePicker.getTime().toISOString()"/>

However, even though the string gets correctly generated (at least that's how the JSON looks like): 但是,即使正确生成了string (至少这就是JSON的样子):

"time": "2017-11-14T10:30:00.271Z"

I'm getting this error: 我收到此错误:

CreateGroupComponent.html:40 ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: '2017-11-14T10:30:00.338Z'. Current value: '2017-11-14T10:30:00.341Z'.
    at viewDebugError (core.js:9514)
    at expressionChangedAfterItHasBeenCheckedError (core.js:9492)
    at checkBindingNoChanges (core.js:9661)
    at checkNoChangesNodeInline (core.js:13674)
    at checkNoChangesNode (core.js:13646)
    at debugCheckNoChangesNode (

I see this error only if I call toISOString() . 仅当我调用toISOString()我才会看到此错误。 If I just return the Date object it's fine but converting it to an ISO string gives me that exception. 如果我只返回Date对象,那很好,但是将其转换为ISO字符串会给我该异常。 Why? 为什么?


Talking about toISOString() - where's actually the timezone information in 谈论toISOString() -实际的时区信息在哪里

2017-11-14T10:30:00.271Z

? For me, it's actually supposed to be 对我来说,实际上应该是

2017-11-14T10:30:00.271Z+01:00

This happens because milliseconds change on each getTime call, this is what the error says. 发生这种情况是因为每个getTime调用的毫秒数都发生了变化,这就是错误的含义。

It should be: 它应该是:

getTime(): Date {
  const date = new Date();
  date.setHours(this.hour);
  date.setMinutes(this.minute);
  date.setSeconds(0);
  date.setMilliseconds(0);  
  return date;
}

Regarding timezone, the reference states, 关于时区, 参考指出,

The toISOString() method returns a string in simplified extended ISO format (ISO 8601) <...>. toISOString()方法以简化的扩展ISO格式(ISO 8601)<...>返回字符串。 The timezone is always zero UTC offset , as denoted by the suffix "Z". 时区始终为零UTC偏移量 ,如后缀“ Z”所示。

If local time is expected, see this question for possible solutions. 如果需要当地时间,请参阅此问题以获取可能的解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 cdkdrag - Angular ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. cdkdrag Angular — ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 (嵌套的FormArray) - Angular — ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. (Nested FormArray) ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Angular - ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Angular ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 以前的值:“未定义” - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined' ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 在 mat-tab-group 上使用 Ngclass - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. with Ngclass on mat-tab-group ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 以前的值:&#39;ngIf:true&#39;。 当前值:&#39;ngIf:false&#39; - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: true'. Current value: 'ngIf: false' ExpressionChangedAfterItHasBeenCheckedError:检查后表达式已更改。 以前的值:“未定义”异常 - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'undefined' Exception ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 先前的值:&#39;ngIf:false&#39;。 当前值:&#39;ngIf:true&#39; - ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. Previous value: 'ngIf: false'. Current value: 'ngIf: true' 检查后表情发生了变化。 - Expression has changed after it was checked. Angular 4:`ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后发生了变化 - Angular 4: `ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM