Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to show JSON on codemirror?

currently I am using ngx-codemirror - a angular wrapper on the codemirror.

in the my app

html as show

<ngx-codemirror [(ngModel)]="data" [autoFocus]=true [options]="{lineNumbers: true,theme: 'material',mode: 'markdown'}"></ngx-codemirror>

ts as show

data='';
   json = {
        "title": "Type",
        "description": "A type of product",
        "type": "object"
  };

constructor(){
      this.data = JSON.stringify(this.json);
}

it shows correctly the json text in a single line, but I want to show it in a json format instead of a string within a single line.

how would I do that?

like image 628
user824624 Avatar asked Oct 15 '25 13:10

user824624


1 Answers

You could send in an empty whitespace character as the space parameter in the JSON.stringify() method. Try the following

Controller

data = '';
json = {
  "title": "Type",
  "description": "A type of product",
  "type": "object"
};

ngOnInit() {
  this.data = JSON.stringify(this.json, null, ' ');    // <-- string literal ' ' as `space` argument  
}

You could also modify the indentation space by sending a number. For eg. JSON.stringify(this.json, null, 4) denotes an indentation of 4 spaces.

Template

<ngx-codemirror #codemirror
  [options]="codeMirrorOptions"
  [(ngModel)]="data"
  (ngModelChange)="setEditorContent($event)">
</ngx-codemirror>

Working example: Stackblitz

like image 70
ruth Avatar answered Oct 17 '25 07:10

ruth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!