Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a json-formatter (directive/component) for angular?

Tags:

angular

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?

like image 677
Michael K. Avatar asked Aug 02 '17 16:08

Michael K.


1 Answers

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());
    }
  }
}

Plunker Example

or create the same component for angular2

Plunker Example

like image 165
yurzui Avatar answered Sep 28 '22 01:09

yurzui