简体   繁体   English

Angular DOM 更新问题,直到加载子组件

[英]Angular DOM update issue until sub-component is loaded

I am trying to start a loader (spinner) before loading a sub-component.我正在尝试在加载子组件之前启动加载器(微调器)。 I am setting the visibility of loader div to display block on click action of a button.我正在设置加载器 div 的可见性以在按钮的单击操作上显示块。 And trying to stop the loader once sub-component is loaded completly, that is in ngAfterViewInit of sub-component.并在子组件完全加载后尝试停止加载器,即在子组件的 ngAfterViewInit 中。

But loader is never rendered.但是 loader 永远不会被渲染。

And its not just about loader, if you update any DOM poroperties on click of the button, it wont be updated in UI.而且它不仅仅是关于加载器,如果您在单击按钮时更新任何 DOM 端口,它不会在 UI 中更新。

I thought some problem in my application, so took an example from angular.io and updated it with the needed loader.我认为我的应用程序有问题,所以从 angular.io 中举了一个例子,并用所需的加载器对其进行了更新。 Still it behaves the same.它的行为仍然相同。

Playground with tour of heros is in stackblitz for tetsing.带有英雄之旅的游乐场在stackblitz中进行测试。

I also found a bug with ngAfterViewInit , but it was in NativeScript repository, so angular team had not paid any attention to it I hope.我还发现了ngAfterViewInit的一个错误,但它在 NativeScript 存储库中,所以我希望 angular 团队没有注意到它。

Please inform me what is the right way to update DOM for these kind of cases or if it is a issue with angular I will raise with them.请告诉我针对此类情况更新 DOM 的正确方法是什么,或者如果这是 angular 的问题,我会向他们提出。

In the stackblitz playground I had put a alert in ngAfterViewInit to varify if loader content is visible, actually if I run that project locally firefox is showing the loader with alert, after removing the alret firefox is also not able to show the loader.在 stackblitz 操场上,我在 ngAfterViewInit 中设置了一个警报,以验证加载程序内容是否可见,实际上,如果我在本地运行该项目 firefox 会显示带有警报的加载程序,删除警报 firefox 后也无法显示加载程序。

Code Flow: dashboard is the component having loader div and click event to start the loader.代码流程:仪表板是具有加载器 div 和单击事件以启动加载器的组件。 hero-detail component's ngAfterViewInit is emitting a event to stop the loader. hero-detail 组件的 ngAfterViewInit 正在发出一个事件来停止加载程序。

Try to use "hidden" instead of ngIF ( [hidden]="heroDetail").尝试使用“hidden”而不是 ngIF([hidden]="heroDetail")。 In short, ngIf will add or remove the element from DOM, then the DOM events will be not tracked as you expect.简而言之,ngIf 将从 DOM 中添加或删除元素,然后不会像您期望的那样跟踪 DOM 事件。 Hidden will hide or show the element via CSS keeping all its event listeners. Hidden 将通过 CSS 隐藏或显示元素,保留其所有事件侦听器。

You can read more about in:你可以阅读更多关于:

https://www.angularjswiki.com/angular/difference-between-ngif-and-hidden-or-displaynone-in-angular/ https://www.angularjswiki.com/angular/difference-between-ngif-and-hidden-or-displaynone-in-angular/

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

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