简体   繁体   English

如何确保 ngAfterViewInit 在呈现某个 HTML 元素后运行?

[英]How to make sure ngAfterViewInit runs after a certain HTML element is rendered?

In HTML, there is a <a> that redirects to another component, and after that scrolls to a certain anchor.在 HTML 中,有一个<a>重定向到另一个组件,然后滚动到某个锚点。

In the target component, I have the following code在目标组件中,我有以下代码

  ngAfterViewInit(): void {
    this.route.fragment.subscribe(fragment => {
      const elmntLoc: HTMLElement = document.getElementById(fragment);
      window.scroll({
        top: elmntLoc.offsetTop,
        behavior: "smooth"
      });
    });
  }

However, I found that document.getElementById(fragment);但是,我发现document.getElementById(fragment); is always null , because this element is displayed conditionally using ngIf = booleanVariable , and when this ngAfterViewInit life cycle hook runs, this booleanVariable is not calculated yet.始终为null ,因为此元素使用ngIf = booleanVariable有条件地显示,并且当此ngAfterViewInit生命周期挂钩运行时,此booleanVariable尚未计算。

I am wondering how to make sure ngAfterViewInit runs after booleanVariable is calculated and therefore this element is rendered?我想知道如何确保ngAfterViewInit在计算booleanVariable并因此呈现该元素之后运行?

I tried to use setTime but it seems hacky...我尝试使用setTime但它似乎很hacky......

Thanks in advance!提前致谢!

Try using _elementRef instead:尝试使用 _elementRef 代替:

this._elementRef.nativeElement.querySelector('#'+fragment)

You could try waiting for the next animation frame to give a chance for the html element to render.您可以尝试等待下一个 animation 帧给 html 元素渲染的机会。

ngAfterViewInit(): void {
  this.route.fragment.subscribe(fragment => {
    window.requestAnimationFrame(_ => {
      const elmntLoc: HTMLElement = document.getElementById(fragment);
      window.scroll({
        top: elmntLoc.offsetTop,
        behavior: "smooth"
      });
    }
  });
}

Need to see more of your code to give a more robust answer.需要查看更多代码才能给出更可靠的答案。

Thanks for the answers but they don't work properly I am afraid... (Perhaps I tried them in a wrong way.)感谢您的回答,但恐怕它们无法正常工作......(也许我以错误的方式尝试了它们。)

I managed to solve this problem using我设法解决了这个问题

  @ViewChild('elementId') set elementName(elementName: ElementRef) {
      # code to scroll to a certain section of the page.
  };

The method will run after this ViewChild is rendered.该方法将ViewChild呈现后运行。

暂无
暂无

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

相关问题 如何确保HTML元素位于窗口的中心? - How to make sure an HTML element is in the center of the window? 显示时如何确保 HTML 元素在屏幕中? - How to make sure an HTML element is in the screen when shown? JS:如何确保只有一个 html 元素可见 - JS: how to make sure only one html element is visible 在此get函数完全完成并呈现之后,如何确保运行其他函数? - How can I make sure that other functions is run after this get function is fully completed and rendered? 如何确保在渲染DOM之后调用我的dom操作函数/代码? - How to make sure my dom manipulating function/code is called after the DOM has been rendered? 如何确保firebase数据库的onUpdate方法只在onCreate方法完成后运行 - How to make sure that the onUpdate method of the firebase database only runs after the onCreate method completes 如何保证在保存后可以运行方法? - How can I use to promise to make sure an method runs after it saves? 如何确保在前一个块完成后运行一个 JavaScript 块? - How do I make sure a block of JavaScript runs after previous block has finished? 从setInterval回调调用时,如何确保在弹出“警告”之前呈现HTML内容? - How do I make sure the HTML content is rendered before `alert` pops up when calling from a setInterval callback? 如何确保只有在某个事件完成后 props 的变化才会触发渲染? - How to make sure that change in props trigger a render only after a certain event is completed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM