简体   繁体   English

使用滚动事件时的 ExpressionChangedAfterItHasBeenCheckedError

[英]ExpressionChangedAfterItHasBeenCheckedError when using scroll event

In my website, the logo rotate when user scroll in the page.在我的网站中,当用户在页面中滚动时,徽标会旋转。

The code is very basic:代码非常基本:

Component零件

@HostListener('window:scroll', ['$event'])
scrollPos(){
  if (typeof window !== 'undefined') {
    return Math.round(window.scrollY);
  }
}

HTML HTML

<img src="assets/logo.svg" [ngStyle]="{ transform: 'rotate(' + scrollPos() + 'deg)' }" />

However, sometimes this basic code generate this error when navigating in the website:(但是,有时此基本代码在网站中导航时会产生此错误:(

 ERROR Error: NG0100: ExpressionChangedAfterItHasBeenCheckedError: Expression has changed after it 
 was checked. Previous value: 'rotate(397deg)'. Current value: 'rotate(331deg)'.

How can I update my above codes in order to avoid having this error?如何更新上述代码以避免出现此错误?

thank you for your support谢谢您的支持

You should generally avoid returning things from @HostListeners because... they're not supposed to be called by anything but the events they listen to;您通常应该避免从@HostListeners返回东西,因为......除了他们收听的事件之外,它们不应该被任何东西调用;

Instead, assign window.scrollY to a property in your component and access it from your component;相反,将window.scrollY分配给组件中的属性并从组件中访问它;

public wScrollY: number = 0;

@HostListener('window:scroll', ['$event'])
scrollPos(){
  if (typeof window !== 'undefined') {
    this.wScrollY = Math.round(window.scrollY);
  }
}

Template模板

<img src="assets/logo.svg" [ngStyle]="{ transform: 'rotate(' + wScrollY + 'deg)' }" />

暂无
暂无

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

相关问题 使用共享组件时的 ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when using shared component 使用异步 pipe 时的 ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when using async pipe 使用 ReactiveForms 和 NgbModal 时的 ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when using ReactiveForms and NgbModal 使用 ChildView 时如何修复 ExpressionChangedAfterItHasBeenCheckedError? - How to fix ExpressionChangedAfterItHasBeenCheckedError when using ChildView? 在窗体内使用Tab时出现ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when using Tab inside a Form 使用 *ngFor 和 ContentChildren 时出现 ExpressionChangedAfterItHasBeenCheckedError - Got ExpressionChangedAfterItHasBeenCheckedError when using *ngFor and ContentChildren 即使通过 @Output 事件更改属性,也会出现 ExpressionChangedAfterItHasBeenCheckedError - Getting ExpressionChangedAfterItHasBeenCheckedError even when changing the property through @Output event 在 textarea 上使用 [(ngModel)] 绑定时如何避免 ExpressionChangedAfterItHasBeenCheckedError - How to avoid ExpressionChangedAfterItHasBeenCheckedError when using [(ngModel)] binding on textarea Angular 反应性 Forms AddControl 在使用 PatchValue 时导致 ExpressionChangedAfterItHasBeenCheckedError - Angular Reactive Forms AddControl Causes ExpressionChangedAfterItHasBeenCheckedError When Using PatchValue 注销时的 ExpressionChangedAfterItHasBeenCheckedError - ExpressionChangedAfterItHasBeenCheckedError when logging out
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM