在 JavaScript 中,如果一次发送一个则添加到数组
In JavaScript, add to array if sending one at a time
最终我需要显示数组中的列表(如果有多个)。我正在捕获错误,然后显示出现错误的字符串列表(尝试下载)。
这些是来自 Observable 的字符串,一次发送一个字符串,因此数组的长度始终为 1,并显示最后发送的值。我如何继续填充这个数组?
(使用Angular9,没有jQuery)
error.service...
public errorDownloadingDoc$ = new Subject<any>();
// getDocError gets called multiple times in one request, for each doc requested (to download), handles one doc at a time
getDocError(doc): Observable<boolean> {
return new Observable((observer: Observer<boolean>) => {
this.dataService.getDoc(doc).subscribe((content) => {
if (content) {
observer.next(true);
observer.complete();
}
}, (error) => {
if (error) {
const allErrorsArrary = [];
allErrorsArrary.push(doc);
console.log(allErrorsArrary); // trying to collect all the errors into one array
this.errorDownloadingDoc$.next(allErrorsArrary); // sends to component
}
}
);
});
}
component.ts...
ngOnInit() {
this.subscriptions.push(
this.offlineZipDownloadService.errorDownloadingDoc$.subscribe(
(allErrorsArrary) => {
this.docNotDownloadedFromError = allErrorsArrary;
}
)
);
}
html...
<div *ngFor="let err of docNotDownloadedFromError">
<P>Documents: {{ err }} </P>
</div>
所以您想要的是 error.service 有一个累积错误的数组?
尝试在外部定义数组,现在每次发送错误时你 re-define 它..
像这样(假设其他一切正常):
public errorDownloadingDoc$ = new Subject<any>();
const allErrorsArrary = [];
getDocError(doc): Observable<boolean> {
return new Observable((observer: Observer<boolean>) => {
this.dataService.getDoc(doc).subscribe((content) => {
if (content) {
observer.next(true);
observer.complete();
}
}, (error) => {
if (error) {
allErrorsArrary.push(doc);
console.log(allErrorsArrary);
this.errorDownloadingDoc$.next(allErrorsArrary);
}
}
);
});
}
最终我需要显示数组中的列表(如果有多个)。我正在捕获错误,然后显示出现错误的字符串列表(尝试下载)。
这些是来自 Observable 的字符串,一次发送一个字符串,因此数组的长度始终为 1,并显示最后发送的值。我如何继续填充这个数组?
(使用Angular9,没有jQuery)
error.service...
public errorDownloadingDoc$ = new Subject<any>();
// getDocError gets called multiple times in one request, for each doc requested (to download), handles one doc at a time
getDocError(doc): Observable<boolean> {
return new Observable((observer: Observer<boolean>) => {
this.dataService.getDoc(doc).subscribe((content) => {
if (content) {
observer.next(true);
observer.complete();
}
}, (error) => {
if (error) {
const allErrorsArrary = [];
allErrorsArrary.push(doc);
console.log(allErrorsArrary); // trying to collect all the errors into one array
this.errorDownloadingDoc$.next(allErrorsArrary); // sends to component
}
}
);
});
}
component.ts...
ngOnInit() {
this.subscriptions.push(
this.offlineZipDownloadService.errorDownloadingDoc$.subscribe(
(allErrorsArrary) => {
this.docNotDownloadedFromError = allErrorsArrary;
}
)
);
}
html...
<div *ngFor="let err of docNotDownloadedFromError">
<P>Documents: {{ err }} </P>
</div>
所以您想要的是 error.service 有一个累积错误的数组? 尝试在外部定义数组,现在每次发送错误时你 re-define 它.. 像这样(假设其他一切正常):
public errorDownloadingDoc$ = new Subject<any>();
const allErrorsArrary = [];
getDocError(doc): Observable<boolean> {
return new Observable((observer: Observer<boolean>) => {
this.dataService.getDoc(doc).subscribe((content) => {
if (content) {
observer.next(true);
observer.complete();
}
}, (error) => {
if (error) {
allErrorsArrary.push(doc);
console.log(allErrorsArrary);
this.errorDownloadingDoc$.next(allErrorsArrary);
}
}
);
});
}