1

我有一个 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]
    }

有什么帮助吗?谢谢

4

1 回答 1

2

您可以指定缩进:

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

活生生的例子

于 2017-01-19T23:16:50.983 回答