Trying to write an Angular 2 pipe that will take a JSON object string and return it pretty-printed/formatted to display to the user.
For example, it would take this:
I would create a custom pipe for this:
@Pipe({
name: 'prettyprint'
})
export class PrettyPrintPipe implements PipeTransform {
transform(val) {
return JSON.stringify(val, null, 2)
.replace(' ', ' ')
.replace('\n', '
');
}
}
and use it this way:
@Component({
selector: 'my-app',
template: `
`,
pipes: [ PrettyPrintPipe ]
})
export class AppComponent {
obj = {
test: 'testttt',
name: 'nameeee'
}
}
See this stackblitz: https://stackblitz.com/edit/angular-prettyprint