Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 角度重复对象的动态数组_Javascript_Angularjs_Json_Multidimensional Array - Fatal编程技术网

Javascript 角度重复对象的动态数组

Javascript 角度重复对象的动态数组,javascript,angularjs,json,multidimensional-array,Javascript,Angularjs,Json,Multidimensional Array,给定对象数组的json,如何使用ng repeat动态显示它错误键是静态的/始终保持不变。只有error的值会更改。因此,如果密码字段出错,那么我将在错误 { "error": { "firstName": { "0": "The first name field is required.", "1": "The first name field must have 2-5 characters." }, "lastName": {

给定对象数组的json,如何使用ng repeat动态显示它<代码>错误键是静态的/始终保持不变。只有
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."
    }
  }
}

我试着做:

<ul ng-repeat="message in errormessage">
    <li ng-repeat="(k,v) in message">
         {{k}} - {{v}}
    </li>
</ul>
编辑

我想你首先要考虑的是,如果你对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."
        }
    }
}
为什么这样做是因为在同一个对象或数组上不能有两个名为error的字段。在此之后,提供的第一个解决方案必须起作用

<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>

  • {{k}}:
    • {{err}}
angular.module('app',[])
.controller('SampleCtrl',函数($scope){
$scope.errormessage={
“错误”:{
“名字”:{
“0”:“名字段是必需的。”,
“1”:“名字字段必须有2-5个字符。”
},
“姓氏”:{
“0”:“姓氏字段是必需的。”
},
“passowrd”:{
“0”:“密码字段是必需的。”
}
}
};
});

  • {{k}}:
    • {{err}}

确保在正确的对象上使用ng repeat。你有:

ng-repeat="message in errormessage"

errormessage
的内容似乎是
“[]”
。参见一些观察结果:

  • 给定对象数组的json
    。您的
    JSON
    不是对象的
    数组
  • 尝试此
    ng repeat=“错误消息中的消息。错误”
    而不是
    ng repeat=“错误消息中的消息”
演示

var myApp=angular.module('myApp',[]);
myApp.controller('MyCtrl',函数($scope){
$scope.errormessage={
“错误”:{
“名字”:{
“0”:“名字段是必需的。”,
“1”:“名字字段必须有2-5个字符。”
},
“姓氏”:{
“0”:“姓氏字段是必需的。”
}
}
}
});

  • {{k}}-{v}

您的JSON无效。为什么它不是有效的JSON?请使用在线验证程序进行验证。再试一次。这里有两个json。一个也没有。那就对了。最好解释一下让它工作需要什么,而不是设置一个不工作的演示。他们已经有了一个不工作的版本,看起来很奇怪,它返回的是字符串而不是数组。类似以下内容:{code>{“error”:{“firstName”:{“0”:“firstName字段是必需的。”,“1”:“firstName字段必须有2-5个字符。”},“lastName”:{“0”:“lastName字段是必需的。”},“passowrd”:{“0”:“password字段是必需的。”}}
出现此错误
错误:ngRepeat:dups replice Key in Repeater
使用
track by$index
将每个字母显示为一个数组。听起来像是在字符串中循环,而不是在有效对象中循环。您可能需要解析JSON字符串。使用
JSON.parse(您的错误\u响应)
<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>
ng-repeat="message in errormessage"