简体   繁体   English

在angularjs codemirror中显示json对象

[英]Display json object in angularjs codemirror

I have a json object and want to display it on codemirror using angularjs codemirror but it got an error saying ui-codemirror cannot use an object or an array as a model . 我有一个json对象,并想使用angularjs codemirror在codemirror上显示它,但是出现一个错误,说ui-codemirror cannot use an object or an array as a model Then I tried to convert the object into string using JSON.stringify , the string does not format nicely in codemirror. 然后,我尝试使用JSON.stringify将对象转换为字符串,该字符串在codemirror中的格式不正确。 Anyone can help me to figure out how to make my code in formatted nicely in codemirror? 任何人都可以帮助我弄清楚如何在Codemirror中很好地格式化我的代码? Thanks 谢谢

ex: 例如:

//inside javascript

    $scope.code = {
        "name": "user1",
        "id": "34",
        "value": [3, 5, 4]
    };

     $scope.editorOptions = {
          lineWrapping : true,
          lineNumbers: true,
          mode: 'application/json',
        };

//inside html
<ui-codemirror ui-codemirror-opts="editorOptions" ng-model="code"></ui-codemirror>

It returns an error for this: ui-codemirror cannot use an object or an array as a model 它为此返回错误:ui-codemirror不能将对象或数组用作模型

if I change to JSON.stringify($scope.code) , code mirror display like this: 如果我更改为JSON.stringify($scope.code) ,则代码镜像显示如下:

{"name":"user1","id":"34","value":[3,5,4]} { “名称”: “USER1”, “ID”: “34”, “值”:[3,5,4]}

However, I want it to display as this: 但是,我希望它显示为:

{ {

  "name": "user1", "id": "34", "value": [3, 5, 4] } 

Any help? 有什么帮助吗? Thanks 谢谢

You can specify the indentation: 您可以指定缩进:

$scope.codeView = JSON.stringify($scope.code, null, 4);

Live example 现场例子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM