admin管理员组文章数量:1026989
get jsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
set jsonString(data: string) {
this.data = JSON.parse(data);
}
JSON.stringify is taking too long to complete since my data is huge and it's blocking my UI, I'm using a spinner loader and it's stuck . I want the loader to spin till the data is ready to show
<button id="btnGetJson" pButton icon="fa fa-file-code-o" pTooltip="Json Viewer" tooltipPosition="bottom" class="p-button-secondary" (click)="handleJsonViewerOpen()" ></button>
<p-dialog [(visible)]="jsonView" header="Json Viewer({{ header }})" positionTop="0" styleClass="custom-dialog" responsive="true" [closable]="false" >
<loader [visible]="loading" *ngIf="!jsonViewerLoader"></loader>
get jsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
set jsonString(data: string) {
this.data = JSON.parse(data);
}
JSON.stringify is taking too long to complete since my data is huge and it's blocking my UI, I'm using a spinner loader and it's stuck . I want the loader to spin till the data is ready to show
<button id="btnGetJson" pButton icon="fa fa-file-code-o" pTooltip="Json Viewer" tooltipPosition="bottom" class="p-button-secondary" (click)="handleJsonViewerOpen()" ></button>
<p-dialog [(visible)]="jsonView" header="Json Viewer({{ header }})" positionTop="0" styleClass="custom-dialog" responsive="true" [closable]="false" >
<loader [visible]="loading" *ngIf="!jsonViewerLoader"></loader>
Share
Improve this question
edited Nov 18, 2024 at 12:27
JSON Derulo
18k11 gold badges57 silver badges75 bronze badges
asked Nov 18, 2024 at 10:56
AskyyAskyy
191 bronze badge
2 Answers
Reset to default 0Both JSON.stringify
and JSON.parse
are resource intensive operations when working with large datasets, you performance might be impacted.
But the real problem is using this logic in the getter and setter
methods, so when you define logic inside get
, the logic gets called in every change detection cycle, so the logic get's called a lot of times.
Also you are calling this.cdr.markForCheck();
inside the get
method which might be creating an infinite loop of change detection cycles.
The solution is to convert the getter and setter
into ordinary methods, so that they do not get called for each change detection cycle.
getJsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
setJsonString(data: string) {
this.data = JSON.parse(data);
}
Not correct way to call method from DOM, i suggest to create an obs like BehaviorSubject and setter have just to update this one.
protected jsonStringify = new Subject<string>() // or Behavior
setJsonString(data: string) {
this.jsonStringify.next(JSON.stringify(JSON.parse(data), null, '\t'));
this.loading = false;
}
get jsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
set jsonString(data: string) {
this.data = JSON.parse(data);
}
JSON.stringify is taking too long to complete since my data is huge and it's blocking my UI, I'm using a spinner loader and it's stuck . I want the loader to spin till the data is ready to show
<button id="btnGetJson" pButton icon="fa fa-file-code-o" pTooltip="Json Viewer" tooltipPosition="bottom" class="p-button-secondary" (click)="handleJsonViewerOpen()" ></button>
<p-dialog [(visible)]="jsonView" header="Json Viewer({{ header }})" positionTop="0" styleClass="custom-dialog" responsive="true" [closable]="false" >
<loader [visible]="loading" *ngIf="!jsonViewerLoader"></loader>
get jsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
set jsonString(data: string) {
this.data = JSON.parse(data);
}
JSON.stringify is taking too long to complete since my data is huge and it's blocking my UI, I'm using a spinner loader and it's stuck . I want the loader to spin till the data is ready to show
<button id="btnGetJson" pButton icon="fa fa-file-code-o" pTooltip="Json Viewer" tooltipPosition="bottom" class="p-button-secondary" (click)="handleJsonViewerOpen()" ></button>
<p-dialog [(visible)]="jsonView" header="Json Viewer({{ header }})" positionTop="0" styleClass="custom-dialog" responsive="true" [closable]="false" >
<loader [visible]="loading" *ngIf="!jsonViewerLoader"></loader>
Share
Improve this question
edited Nov 18, 2024 at 12:27
JSON Derulo
18k11 gold badges57 silver badges75 bronze badges
asked Nov 18, 2024 at 10:56
AskyyAskyy
191 bronze badge
2 Answers
Reset to default 0Both JSON.stringify
and JSON.parse
are resource intensive operations when working with large datasets, you performance might be impacted.
But the real problem is using this logic in the getter and setter
methods, so when you define logic inside get
, the logic gets called in every change detection cycle, so the logic get's called a lot of times.
Also you are calling this.cdr.markForCheck();
inside the get
method which might be creating an infinite loop of change detection cycles.
The solution is to convert the getter and setter
into ordinary methods, so that they do not get called for each change detection cycle.
getJsonString(): string {
this.loading = false;
this.cdr.markForCheck();
return JSON.stringify(this.data, null, '\t');
}
setJsonString(data: string) {
this.data = JSON.parse(data);
}
Not correct way to call method from DOM, i suggest to create an obs like BehaviorSubject and setter have just to update this one.
protected jsonStringify = new Subject<string>() // or Behavior
setJsonString(data: string) {
this.jsonStringify.next(JSON.stringify(JSON.parse(data), null, '\t'));
this.loading = false;
}
本文标签: angularJSONstringify blocking UI loaderStack Overflow
版权声明:本文标题:angular - JSON.stringify blocking UI loader - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745623907a2159743.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论