简体   繁体   English

Angular Mat-Slide-Toggle 响应缓慢

[英]Angular Mat-Slide-Toggle Slow to Respond

I have a simple control panel which has an Angular Mat-Slide-Toggle control as follows:我有一个简单的控制面板,它有一个 Angular Mat-Slide-Toggle 控件,如下所示:

<mat-slide-toggle (change)="onQAStateDisplayChanged($event)">Display QA Status</mat-slide-toggle>

The function called on the change event simply fires an event as follows:change事件上调用的函数只是简单地触发一个事件,如下所示:

@Output() qaStateDisplayChanged: EventEmitter<boolean> = new EventEmitter();
...
onQAStateDisplayChanged(event: MatSlideToggleChange) {
    this.qaStateDisplayChanged.emit(event.checked);
}

My container component, listening for the event, calls a function which takes several seconds to complete.我的容器组件监听事件,调用一个需要几秒钟才能完成的函数。 I had hoped that on toggling the SlideToggle the qaStateDisplayChanged event would be emitted and the SlideToggle would immediately slide across.我曾希望在切换 SlideToggle 时会发出qaStateDisplayChanged事件,并且 SlideToggle 会立即滑过。 What actually happens is you click the SlideToggle and nothing visually changes until everything completes several seconds later, which is not a great user experience!实际发生的情况是您单击 SlideToggle 并且在几秒钟后一切完成之前没有任何视觉变化,这不是很好的用户体验!

Following the event being received a series of 3D model operations take place, using Three.js.在接收到事件之后,使用 Three.js 进行一系列 3D 模型操作。 It appears as if the browser is unable to refresh the SlideToggle because it is intensively manipulating the 3D model.看起来好像浏览器无法刷新 SlideToggle,因为它正在密集地操纵 3D 模型。 Once the model has updated the browser can update the UI and the SlideToggle slides across.模型更新后,浏览器可以更新 UI,并且 SlideToggle 会滑动。

Can anyone suggest an alternative approach which would be more resonsive?任何人都可以提出一种更能引起共鸣的替代方法吗?

I see two problems here:我在这里看到两个问题:

  1. During heavy calculations UI is not responsive.在繁重的计算期间,UI 没有响应。 It is just a fact.这只是一个事实。
  2. Slide Toggle does not have enough time to change its UI. Slide Toggle 没有足够的时间来更改其 UI。

I can propose workaround for the second problem.我可以为第二个问题提出解决方法。 You can disable animation and use setTimeout().您可以禁用动画并使用 setTimeout()。

Add to your component:添加到您的组件:

providers: [
    {provide: ANIMATION_MODULE_TYPE, useValue: 'NoopAnimations'}
]

And use something like并使用类似的东西

onQAStateDisplayChanged(event: MatSlideToggleChange) {
    setTimeout(() => this.qaStateDisplayChanged.emit(event.checked), 80);
}

You can see full working example here https://stackblitz.com/edit/angular-qmjfuh您可以在此处查看完整的工作示例https://stackblitz.com/edit/angular-qmjfuh

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

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