简体   繁体   English

如何使用角度js正确显示JSON响应?

[英]How do I properly display a JSON response using angular js?

The following is a json response returned to angular js for error codes: 以下是为错误代码返回角度js的json响应:

{"email":["The email field is required."],"username":["The username field is required."],"password":["The password field is required."]}

I am binding a variable in my controller to the html to display the result: 我将控制器中的变量绑定到html以显示结果:

angular js: 角js:

.success(function(data) {
  $scope.errors = data;
});

html: HTML:

{{ errors }}

The html result of this is: 这个的html结果是:

{"email":["The email field is required."],"username":["The username field is required."],"password":["The password field is required."]} {“email”:[“电子邮件字段为必填项。”],“用户名”:[“用户名字段为必填项。”],“密码”:[“密码字段为必填项。”]}

How do I get rid of the brackets, quotes, ect. 如何摆脱括号,引号等。 so that I see just the data? 所以我只看到数据? I feel like this is a silly question, but I can't seem to find a good answer. 我觉得这是一个愚蠢的问题,但我似乎无法找到一个好的答案。 Thanks! 谢谢!

You should specify property which you want to display. 您应该指定要显示的属性。 And since you have an arrays in each property you can use ng-repeat to display data: 由于每个属性中都有一个数组,因此可以使用ng-repeat来显示数据:

<p ng-repeat="error in errors.email">{{ error }}</p>

<p ng-repeat="error in errors.username">{{ error }}</p>

<p ng-repeat="error in errors.password">{{ error }}</p>

Fiddle 小提琴

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

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