AngularJS格式JSON字符串输出 [英] AngularJS format JSON string output
问题描述
我有AngularJS应用程序,它从输入收集数据,使用模型转换成字符串 JSON.stringify()
键,让用户编辑在这样的方式这个模型输入字段得到更新,如果< TextArea>
元素被更新,反之亦然。某种双向绑定:)
I have AngularJS application, which collects data from input, transforms model into string using JSON.stringify()
and lets a user edit this model in such 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:
和不喜欢它现在看起来:
And not like it looks now:
任何想法这可怎么acomplished?如果你需要一些额外的信息 - 不要犹豫问。每一个答案是高度AP preciated和immidiately回答。
Any ideas how this can be acomplished? If you need some additional info - don't hesitate asking. Every answer is highly appreciated and answered immidiately.
感谢您。
P.S。我想这应该是某种指令或自定义过滤器。数据本身不应改变,只有输出。
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(值[,替代[,空间]])
参数
- 的值的值转换为一个JSON字符串。
- 的替代的如果一个函数,
转换字符串化时遇到的价值和性能;如果
阵列,用于指定包括在属性集内的对象
最终的字符串。在替代功能的详细说明
JavaScript的指导文章使用本地JSON中提供。 - 的空格的
导致得到的字符串为$打印ptty p $。
- 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," ")
会给你以下结果:
will give you following result:
"{
"a": 1,
"b": 2,
"c": {
"d": 3,
"e": 4
}
}"
这篇关于AngularJS格式JSON字符串输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!