问题描述
我有一个 AngularJS 应用程序,它从输入中收集数据,使用 JSON.stringify()
将模型转换为字符串,并让用户以更新输入字段的方式编辑此模型如果 元素被更新,反之亦然.某种双向绑定:)
I have an AngularJS application, which collects data from input, transforms a model into a string using JSON.stringify()
and lets a user edit this model in such a way that input fields get updated if the <textarea>
element is updated and vice versa. Some kind of two-way binding :)
问题是字符串本身看起来很丑,我想格式化它,使其看起来像这样:
The problem is that the String itself looks ugly and I would like to format it so it looks like this:
和现在的样子不一样:
有什么想法可以实现吗?如果您需要一些其他信息 - 请不要犹豫询问.每个答案都受到高度赞赏并立即得到答复.
Any ideas how this can be accomplished? If you need some additional info - don't hesitate asking. Every answer is highly appreciated and answered immediately.
谢谢.
附言我想这应该是某种指令或自定义过滤器.数据本身不应该被改变,只有输出.
P.S. I guess this should be some kind of directive or a custom filter. Data itself SHOULD NOT be changed, only the output.
推荐答案
您可以使用 JSON.stringify()
JSON.stringify(value[, replacer [, space]])
参数
- value 要转换为 JSON 字符串的值.
- replacer 如果是一个函数,转换字符串化时遇到的值和属性;如果数组,指定包含在对象中的属性集最后的字符串.替换函数的详细描述是使用本机 JSON 的 javaScript 指南文章中提供.
- 空格使结果字符串被漂亮地打印出来.
- value The value to convert to a JSON string.
- replacer If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the final string. A detailed description of the replacer function is provided in the javaScript guide article Using native JSON.
- space Causes the resulting string to be pretty-printed.
例如:
JSON.stringify({a:1,b:2,c:{d:3, e:4}},null," ")
会给你以下结果:
"{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}"
这篇关于AngularJS 格式 JSON 字符串输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!