[英]Angular ng-repeat dynamic array of objects
Given a json of array of objects, how could one display it using a ng-repeat dynamically? 给定对象数组的json,如何使用ng-repeat动态显示它?
error
key is static / always remain the same. error
键是静态的/始终保持不变。 Just the values of error
changes. 只是
error
的值会改变。 So if I have the password field errored-out then I'll have password
key instead on the error
因此,如果我的密码字段输入有误,那么我将使用
password
密钥代替error
{
"error": {
"firstName": {
"0": "The first name field is required.",
"1": "The first name field must have 2-5 characters."
},
"lastName": {
"0": "The last name field is required."
}
}
}
. 。
{
"error": {
"password": {
"0": "The password field is required."
}
}
}
I tried doing: 我试着做:
<ul ng-repeat="message in errormessage">
<li ng-repeat="(k,v) in message">
{{k}} - {{v}}
</li>
</ul>
But it displays this instead: 而是显示为:
0 - [
0 - ]
angular.module('app', []) .controller('SampleCtrl', function($scope) { $scope.errormessage = { "error": { "firstName": { "0": "The first name field is required.", "1": "The first name field must have 2-5 characters." }, "lastName": { "0": "The last name field is required." }, "passowrd": { "0": "The password field is required." } } }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="app" ng-controller="SampleCtrl"> <ul ng-repeat="message in errormessage"> <li ng-repeat="(k,v) in message"> <b>{{k}}:</b><br> <ul> <li ng-repeat="err in v"> {{err}} </li> </ul> </li> </ul> </div>
EDIT 编辑
I think the first thing you have to consider is to normalize your json if you have control over that and represent it like this 我认为您首先要考虑的是规范化json(如果您对此有控制权,并以这种方式表示)
$scope.errormessage = {
"error": {
"firstName": {
"0": "The first name field is required.",
"1": "The first name field must have 2-5 characters."
},
"lastName": {
"0": "The last name field is required."
},
"passowrd": {
"0": "The password field is required."
},
"mail": {
"0": "The first name field is required.",
"1": "The first name field must have 2-5 characters."
},
"newsletter": {
"0": "The newsletter field is required."
},
"address": {
"0": "The address field is required."
}
}
}
Why this because you cant have two field named error on a same object or array. 为什么这样做是因为在同一对象或数组上不能有两个名为error的字段 。 After this the first solution provided must work
之后,提供的第一个解决方案必须可行
<div ng-app="app" ng-controller="SampleCtrl">
<ul ng-repeat="message in errormessage">
<li ng-repeat="(k,v) in message">
<b>{{k}}:</b><br>
<ul>
<li ng-repeat="err in v">
{{err}}
</li>
</ul>
</li>
</ul>
</div>
Make sure you are using the ng-repeat on the right object. 确保在正确的对象上使用ng-repeat。 You have:
你有:
ng-repeat="message in errormessage"
and the content of errormessage
seems to be "[]"
. 和内容
errormessage
似乎是"[]"
。 See http://jsfiddle.net/hgk6vsqv/ 参见http://jsfiddle.net/hgk6vsqv/
Some observations : 一些观察:
Given a json of array of objects
. Given a json of array of objects
。 Your JSON
is not an array of Objects
. JSON
不是array of Objects
。 ng-repeat="message in errormessage.error"
instead of ng-repeat="message in errormessage"
ng-repeat="message in errormessage.error"
而不是ng-repeat="message in errormessage"
DEMO DEMO
var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl',function($scope) { $scope.errormessage = { "error": { "firstName": { "0": "The first name field is required.", "1": "The first name field must have 2-5 characters." }, "lastName": { "0": "The last name field is required." } } } });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <ul ng-repeat="message in errormessage.error"> <li ng-repeat="(k,v) in message"> {{k}} - {{v}} </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.