gpt4 book ai didi

javascript - 如何检查元素是否以 Angular 2渲染

转载 作者:行者123 更新时间:2023-12-01 02:06:06 25 4
gpt4 key购买 nike

我想在表格完全加载后显示一个弹出窗口。我正在使用加载属性,但它不起作用。还有其他方法可以实现这一目标吗?没有亲子概念。

以下是我的代码组件.ts

export class FileUploadComponent {
public files: UploadFile[] = [];
data: AOA = [ [1, 2], [3, 4] ];
wopts: XLSX.WritingOptions = { bookType: 'xlsx', type: 'array' };
fileName: string = 'SheetJS.xlsx';
showData: boolean = false;

public dropped(event: UploadEvent) {
this.files = event.files;
for (const droppedFile of event.files) {
if (droppedFile.fileEntry.isFile) {
const reader: FileReader = new FileReader();
const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
fileEntry.file((file: File) => {
var filePath = file;
reader.onload = (e: any) => {
const bstr: string = e.target.result;
const wb: XLSX.WorkBook = XLSX.read(bstr, {type: 'binary'});
const wsname: string = wb.SheetNames[0];
const ws: XLSX.WorkSheet = wb.Sheets[wsname];
this.data = <AOA>(XLSX.utils.sheet_to_json(ws, {header: 1}));
};
reader.readAsBinaryString(file);
this.showData = true;
this.infoModal.hide();
});
} else {
this.showData = false;
const fileEntry = droppedFile.fileEntry as FileSystemDirectoryEntry;
}
}
}

showPopUp(){
console.log('yes');
}
}

以下是我的component.htm

      <div class="modal-body">
<file-drop id="infoFileModal" headertext="Drop files here" (onFileDrop)="dropped($event)" (onFileOver)="fileOver($event)" (onFileLeave)="fileLeave($event)"></file-drop>
</div>

<div class="card-body">
<div *ngIf="showData" class="upload-table table-responsive">
<table #table class="table-bordered table-striped" id="dest_table" (load)="showPopup()">
<tr *ngFor="let row of data">
<td *ngFor="let val of row">
{{val}}
</td>
</tr>
</table>
</div>
<div *ngIf="!showData" class="div-upload">
No Data Found
</div>
</div>

最佳答案

好的,这是另一种方法。

由于您的表格需要几分钟的时间来渲染,因此在填充数据数组后,您唯一的机会就是在需要的时间内监听更改事件。为了防止每次完成迭代时都会触发 showPopUp() 方法,您可以使用 Observable.debounceTime(),它仅在最后一次更改事件之后过去的时间大于给定时间(以毫秒为单位)时调用该方法。

组件

import { Component, OnInit, OnDestroy, ViewChild, AfterViewInit} from '@angular/core';
import {Observable} from 'rxjs/Observable';
import {Subscription} from "rxjs/Subscription";

@ViewChild('table') table: any;
private timer= Observable.timer(1000); // wait one second before calling the method
private subscription: Subscription;

ngOnDestroy(): void {
if(this.subscription){
this.subscription.unsubscribe();
}
}

public dropped(event: UploadEvent) {
// ...

reader.readAsBinaryString(file);
this.showData = true;
this.infoModal.hide();

// call the watcher with a delay of one second
// in order to give angular enough time to build up the table
this.subscription = this.timer.subscribe( () => {
this.triggerTableWatcher();
});

// ...

}


triggerTableWatcher() {
// define the object to listen to
const trigger = this.table.changes();

// define the debounce-time (adjust it, if it doesn't fit your needs)
const debounceAt = trigger.debounceTime(1000);

// subscribe to the trigger and tell which method to call eventually
debounceAt.subscribe(() => this.showPopUp());
}

HTML-模板(仅重要部分)

<div class="card-body">
<div *ngIf="showData" class="upload-table table-responsive">
<table #table class="table-bordered table-striped" id="dest_table">
<tr *ngFor="let row of data">
<td *ngFor="let val of row">
{{val}}
</td>
</tr>
</table>
</div>

关于javascript - 如何检查元素是否以 Angular 2渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50095384/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com