Angular-有没有办法让活动处理程序等待其执行,直到DOM进行更新?

发布于 2025-01-31 11:31:50 字数 1474 浏览 1 评论 0原文

考虑一个动态表,即一个表格,其行是动态构建的:

<table>
  <tbody>
    <tr *ngFor="let row of tableRows">
      <td>{{ row.id }}</td>
      <td>{{ row.name }}</td>
  </tbody>
</table>

我现在正在为组件的变量tablerows 添加新条目,然后在这样做之后,我发出addrow <addrow < /代码>事件。我使用此表的组件通过将最后一个表行滚动到视图中来处理addrow事件。

我遇到的问题是,我刚刚添加的表行在我调用scrollintoview时尚未在DOM中可用。但是,这似乎是一个毫秒的问题,在将以下SettieMout添加到我的addrow事件处理程序时,一切都可以正常工作:

setTimeout(() => this.scrollRowIntoView(), 0);

我不喜欢必须使用settimeout 在这里。因此,我想知道,是否有一种方法可以等到DOM已更新后才调用scrollintoview()

在我的父组件中实现afterviewChecked时,即包含表和addrow-event handler的组件,我意识到:

ngafterviewchecked()方法在<代码> onaddrow ,即Addrow-event Handler。显然,我希望它是反过来的,并在onaddrow之前调用ngafterviewchecked(),因为只有这样,DOM才进行了更新。现在,我不能只调用onaddrow ins ngafterviewchecked(),因为我不想每次检查视图时都会调用此方法发出。

我设法通过将布尔变量rowadded引入我的父部件来使它起作用,该 在ngafterviewChecked()中被切换。如果此变量为trui,则我调用scrollintoview ngafterviewchecked(),然后切换布尔值变量。但是,为了使其工作,我仍然需要设置要滚动到我的onaddrow事件处理程序的行已经对其进行了检查。

在我看来,无法预见到哪种方法先执行,首先是ngafterviewcheckedonaddrow,这样我倾向于使用settemeTimeout

Consider a dynamic table, i.e. a table, the rows of which are build dynamically:

<table>
  <tbody>
    <tr *ngFor="let row of tableRows">
      <td>{{ row.id }}</td>
      <td>{{ row.name }}</td>
  </tbody>
</table>

I am now adding a new entry to the component's variable tableRows and just after doing so, I emit an addRow event. The component, which I use this table in, handles the addRow event by scrolling the last table row into view.

The issue that I have is that the table row, which I have just added, is not yet available in my DOM at the time that I call scrollIntoView. However, it appears to be a matter of milliseconds, everything works fine when adding the following setTimeOut to my addRow event handler:

setTimeout(() => this.scrollRowIntoView(), 0);

I don't like the idea of having to use a setTimeout here. So, I wonder, is there a way to just wait until the DOM has been updated before calling scrollIntoView() ?

When implementing AfterViewChecked in my parent component, i.e. the component, which contains the table and the addRow-event-handler, I realized:

The ngAfterViewChecked() method is called AFTER onAddRow, i.e. the addRow-event-handler. Obviously, I want it to be the other way round and call ngAfterViewChecked() BEFORE onAddRow, because only then the DOM has been updated. Now, I cannot just call onAddRow inside ngAfterViewChecked() since I do not want to call this method every time the view has been checked, but only if the addRow event has been emitted.

I have managed to get this to work by introducing a boolean variable rowAdded to my parent component, which is toggled inside ngAfterViewChecked(). If this variable is true, I call scrollIntoView inside ngAfterViewChecked() and toggle the boolean variable afterwards. In order for this to work, however, I still need to set the row that I want to scroll to inside my onAddRow event handler, because I set a property, which will be marked as "view changed after it has been checked" if I set it inside ngAfterViewChecked().

It appears to me that it cannot be foreseen which method is executed first ngAfterViewChecked or onAddRow so that I tend to stick with my initial solution with setTimeout.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文