I'm upgrading an app from AngularJS to Angular. In AngularJS I used https://github.com/mohsen1/json-formatter to get displayed a beautified json. Is there an alternative for angular?
-
as workaround <pre>{{something | json}}</pre>Ali– Ali2017-08-02 16:44:01 +00:00Commented Aug 2, 2017 at 16:44
-
2plnkr.co/edit/6Cbs6pIPqGlXM0hmXF9n?p=previewyurzui– yurzui2017-08-02 17:17:18 +00:00Commented Aug 2, 2017 at 17:17
-
@yurzui It works like in my AngularJS-App, thanks. If you post it as answer I will accept it.Michael K.– Michael K.2017-08-03 10:08:43 +00:00Commented Aug 3, 2017 at 10:08
Add a comment
|
1 Answer
You can either use json-formatter-js package
import JSONFormatter from 'json-formatter-js';
@Directive({
selector: 'json-formatter'
})
export class JsonFormatterDirective implements OnChanges {
@Input() json: any;
constructor(private elRef: ElementRef) { }
ngOnChanges() {
if (this.json) {
const formatter = new JSONFormatter(this.json);
this.elRef.nativeElement.appendChild(formatter.render());
}
}
}
or create the same component for angular2