Created
August 30, 2018 09:01
-
-
Save this-is-richard/e9cc467bcb03eb35d130301d48dbc2f8 to your computer and use it in GitHub Desktop.
Angular 6 pretty print json on html (can be array / object)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Component } from '@angular/core'; | |
@Component({ | |
selector: 'app-recursive-object', | |
template: ` | |
<span *ngIf="isArray(items)">[</span> | |
<span *ngIf="!isArray(items)">{{ '{' }}</span> | |
<div *ngFor="let key of objectKeys(items)" class="ml-3"> | |
<span *ngIf="!isArray(items)">{{ key }}: </span> | |
<span *ngIf="!isObject(key)">{{ items[key] }},</span> | |
<app-recursive-object *ngIf="isObject(key)" [items]="items[key]"></app-recursive-object> | |
</div> | |
<span *ngIf="isArray(items)">],</span> | |
<span *ngIf="!isArray(items)">{{ '},' }}</span> | |
` | |
}) | |
export class RecursiveObjectComponent { | |
@Input() items; | |
objectKeys(items) { | |
return Object.keys(items); | |
} | |
isObject(key) { | |
return typeof this.items[key] === 'object'; | |
} | |
isArray(items) { | |
return Array.isArray(items); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment