[英]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.