7

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?

3
  • as workaround <pre>{{something | json}}</pre> Commented Aug 2, 2017 at 16:44
  • 2
    plnkr.co/edit/6Cbs6pIPqGlXM0hmXF9n?p=preview Commented 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. Commented Aug 3, 2017 at 10:08

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.