简体   繁体   English

在AngularJS中格式化JSON对象输出

[英]Formating JSON object output in AngularJS

I have AngularJS web app, which is capable of sending different http methods to our Restful WS. 我有AngularJS网络应用程序,它能够向我们的Restful WS发送不同的http方法。 Each request has a preview, where all of it's properties are listed and can be changed in place. 每个请求都有一个预览,其中列出了所有请求的属性,可以在适当位置进行更改。 Everything works fine except the formating for inputing JSON. 除了用于输入JSON的格式外,其他一切都正常。 It looks like this: 看起来像这样:

在此处输入图片说明

And I would like it to look like this, except that JSON text was displayed like in picture 1: 我希望它看起来像这样,除了JSON文本如图1所示:

在此处输入图片说明

Shortly, I need that the left side was as in 1 pic, and the right side - as in the second pic. 很快,我需要左侧为1张照片,右侧为-如第二张照片。

Here's piece of code, which is responsible for generating input for JSON: 这是一段代码,负责为JSON生成输入:

<div ng-show="field.restResourceName != null">
    <p ng-show={{field.isRequired}}>{{field.name}}*: </p>
    <p ng-show={{!field.isRequired}}>{{field.name}}: </p>
    <accordion id="entityField" close-others="oneAtATime">
        <accordion-group heading={{field.name}} is-open="false">
            <p>JSON:</p>
            <textarea ng-change="getCreateEntityAsText()" ng-model="createEntityResource[field.name]"></textarea>
        </accordion-group>
    </accordion>
</div>

And here's the one, responsible for showing the output: 这是负责显示输出的一个:

<div class="preview">
    <p>Preview: </p>
    <textarea class="form-control" ng-model="createEntityTextAreaModel"></textarea>
</div>

AngularJS controller functions, which parse JSON into model and vice versa: AngularJS控制器函数,可将JSON解析为模型,反之亦然:

//CREATE ENTITY JSON PARSE


$scope.getCreateEntityAsText = function () {
    $scope.createEntityTextAreaModel = JSON.stringify($scope.createEntityResource, null, "    ");
};

$scope.$watch('createEntityTextAreaModel', function () {
    applyParseValues($scope.createEntityTextAreaModel, $scope.createEntityResource);
});

applyParseValues = function(textAreaModel, entityModel){
    if($rootScope.isNotEmptyString(textAreaModel)) {
       angular.copy(JSON.parse(textAreaModel), entityModel);
    } else {
        angular.copy({}, entityModel);
    }
}

Any ideas how this can be achieved? 有什么想法可以实现吗? Every useful answer is highly appreciated and evaluated. 每个有用的答案都受到高度赞赏和评估。

Thank you. 谢谢。

It looks like you're mostly having a styling issue, which is addressed in this question: 您似乎主要遇到样式问题,此问题已解决:

JSON formatter lib JSON格式库

Otherwise, Angular comes with a built-in filter for JSON , which can be used in a view like this: 否则,Angular会为JSON内置一个过滤器 ,可以在如下视图中使用:

<p>{{someObject | json }}</p>

Links: 链接:

https://docs.angularjs.org/api/ng/filter/json https://docs.angularjs.org/api/ng/filter/json https://docs.angularjs.org/api/ng/filter/json https://docs.angularjs.org/api/ng/filter/json

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

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