简体   繁体   English

在DOM改变准备好之前,如何在Angular 7中等待?

[英]How to wait in Angular 7 until DOM changed ready?

I have a list of entries. 我有一个条目列表。 When I click on such an entry a HTTP request is done, data will be loaded and presented in a table. 当我点击这样的条目完成HTTP请求时,将加载数据并将其显示在表格中。 So far so good. 到现在为止还挺好。

Now I try to create an export function to output all tables at once. 现在我尝试创建一个导出函数来一次输出所有表。 The abstracted code looks like this: 抽象代码如下所示:

this.objectlist.forEach(entry => {

    this.getDataFromHTTP(entry).subscribe(response => {
        tabledata.push(this.getTableDOM());
    })

})

In the function getTableDOM() I do something like let table = document.getElementsByClassName(data.classname); 在函数getTableDOM()我执行类似let table = document.getElementsByClassName(data.classname); . The code itself runs when I click every entry. 单击每个条目时代码本身就会运行。 But it doesn't work within the forEach() . 但它在forEach()不起作用。

The real problem is that the getTableDOM() returns an empty result, because the DOM isn't ready when it is called. 真正的问题是getTableDOM()返回一个空结果,因为DOM在调用时没有准备好。

So my question is: how can I wait until DOM changes are done before I call getTableDOM() . 所以我的问题是:在调用getTableDOM()之前,我怎么能等到DOM更改完成。 Or maybe my approach to achieve my goal is completely wrong. 或许我实现目标的方法完全错误。 But if so: how can I do it otherwise? 但如果是这样的话,我怎么能这样做呢?

Thx for your help! 谢谢你的帮助! Lars 拉尔斯

You shouldn't use DOM as a source of data like you are doing. 您不应该像使用DOM那样使用DOM作为数据源。 Anyways, you can detect the changes manually to render the DOM and in next tick export the content. 无论如何,您可以手动检测更改以呈现DOM,然后在下一个tick中导出内容。

constructor(private changeDetector : ChangeDetectorRef) {}

this.objectlist.forEach(entry => {
    this.getDataFromHTTP(entry).subscribe(response => {
        this.changeDetector.detectChanges();//if using push stratagy
        setTimeout(_=>tabledata.push(this.getTableDOM()));
    })
})

Also check this question 还要检查这个问题

You can mark your tables and access them using viewChildren 您可以使用viewChildren标记表并访问它们

@ViewChildren("mytable") mytable;

this.mytable.changes.subscribe((el)=>{
    /*check if desired table node exists on el*/
});

Please see this DEMO I've created, hopefully, it'll get you going in the right direction. 请参阅我创建的这个DEMO ,希望它会让你朝着正确的方向前进。 Like others have mentioned from what you've provided it's difficult to get the full picture. 就像其他人从你提供的内容中提到的那样,很难全面了解情况。 I think what you're looking for is something like OnChanges , I've included relevant example code below. 我认为你正在寻找的东西就像OnChanges ,我在下面列出了相关的示例代码。 I've also included in the demo a forkJoin example. 我还在demo中包含了一个forkJoin示例。

import {
  Component,
  Input,
  OnChanges,
  SimpleChanges
} from '@angular/core';

@Component({
  selector: 'on-change',
  template: `
        <pre>
            {{changes | json}}
        </pre>
    `
})
export class OnChangeComponent implements OnChanges {
  @Input() data: any;
  changes;
  constructor() {}

  ngOnChanges(changes: SimpleChanges) {
    this.changes = changes;
  }
}

This will check every 2 secs ... 这将每2秒检查一次......

public static void AngWait(){
    WebDriverWait jsWait jsExec = (JavascriptExecutor) driver;
    String angular5Check = (String) jsExec.executeScript("return getAllAngularRootElements()[0].attributes['ng-version'].value");
    System.out.println(angular5Check);
    if (angular5Check != null) {
        do {
            angularPageLoaded = (Boolean) jsExec.executeScript("return window.getAllAngularTestabilities().findIndex(x=>!x.isStable()) === -1");
            System.out.println("in loop => "+angularPageLoaded);
            Thread.sleep(2000);

        } while (!angularPageLoaded);

        System.out.println("outside loop => "+angularPageLoaded);
    }

}

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

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