簡體   English   中英

在angularjs codemirror中顯示json對象

[英]Display json object in angularjs codemirror

我有一個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不能將對象或數組用作模型

如果我更改為JSON.stringify($scope.code) ,則代碼鏡像顯示如下:

{ “名稱”: “USER1”, “ID”: “34”, “值”:[3,5,4]}

但是,我希望它顯示為:

{

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

有什么幫助嗎? 謝謝

您可以指定縮進:

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

現場例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM