【问题标题】:Display json object in angularjs codemirror在angularjs codemirror中显示json对象
【发布时间】:2017-01-19 23:01:19
【问题描述】:

我有一个 json 对象,想使用 angularjs codemirror 将其显示在 codemirror 上,但出现错误提示 ui-codemirror cannot use an object or an array as a model。然后我尝试使用JSON.stringify 将对象转换为字符串,该字符串在codemirror 中的格式不正确。任何人都可以帮助我弄清楚如何使我的代码在 codemirror 中格式化得很好? 谢谢

例如:

//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>

它为此返回错误:ui-codemirror cannot use an object or an array as a model

如果我改成JSON.stringify($scope.code),代码镜像显示如下:

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

但是,我希望它显示为:

{

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

有什么帮助吗? 谢谢

【问题讨论】:

  • JSON.stringify($scope.code,null, '\t') ?

标签: javascript angularjs json ui-codemirror


【解决方案1】:

你可以指定缩进:

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

Live example

【讨论】:

  • 如何让一段 json 展开/折叠,类似于我们在在线 json 查看器中看到的内容
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-03
  • 2017-08-29
  • 2016-02-02
相关资源
最近更新 更多