Skip to content

Instantly share code, notes, and snippets.

@this-is-richard
Created August 30, 2018 09:01
Show Gist options
  • Save this-is-richard/e9cc467bcb03eb35d130301d48dbc2f8 to your computer and use it in GitHub Desktop.
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)
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