简体   繁体   English

错误:ExpressionChangedAfterItHasBeenCheckedError 变量 Angular?

[英]Error: ExpressionChangedAfterItHasBeenCheckedError varaible Angular?

I have the following template element:我有以下模板元素:

  <input
   class="form-check-input"
   type="radio"
   name="radio_{{ id }}"
   id="{{ id }}">

And component is:组件是:

  import * as uuid from 'uuid';
  public get id(): number {
       return uuid.v4();
  }

I get this error message:我收到此错误消息:

ERROR Error: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it was checked. ERROR 错误:ExpressionChangedAfterItHasBeenCheckedError:表达式在检查后已更改。 Previous value: '103cb048-c966-4b76-b795-31dd2234204c'.以前的值:'103cb048-c966-4b76-b795-31dd2234204c'。 Current value:当前值:

The component has default strategy.该组件具有默认策略。

'a04eba4a-06b6-400b-af6c-e9afe3040919'. 'a04eba4a-06b6-400b-af6c-e9afe3040919'。

With every change detection cycle, it will call the id() getter, and thus generating a new id.在每个更改检测周期中,它都会调用id() getter,从而生成一个新的 id。 This is most likely not correct, because it will also cause the id and name attribute to be different on the input.这很可能是不正确的,因为它还会导致输入中的idname属性不同。 I think you are looking for this:我想你正在寻找这个:

import * as uuid from 'uuid';
  
public id: string = uuid.v4();

I can however advice you to use the OnPush change detection strategy, as it will give your application a performance boost.但是,我可以建议您使用OnPush更改检测策略,因为它可以提高您的应用程序的性能。 As well as making the change detection more logical以及使变更检测更合乎逻辑

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM