使用ng-repeat时如何调用删除数组参数

时间:2019-06-20 13:49:32

标签: angularjs

在使用ng-repeat时如何调用删除数组对象

 <ul ng-repeat="error in errors">
                <li>{{error}}</li>
            </ul>

在整个过程中不断循环并取得进展。应该只有[]个字符串是可以接受的。

["The password field is required."]

["The password confirmation field is required."]

["The temporary password field is required."]

这是数组响应

{"errors":{"password":["The password field is required
."],"password_confirmation":["The password confirmation field is required."],"temporary_password":["The
 temporary password field is required."]}}

2 个答案:

答案 0 :(得分:0)

您的errors对象似乎包含以字符串数组作为值的对象。您可以将(key, value)ng-repeat一起使用,然后再使用嵌套的ng-repeat。例如:

angular.module('app', [])
  .controller('ctrl', function($scope) {
      $scope.errors = {
            "password": ["The password field is required."],
            "password_confirmation ":["The password confirmation field is required."],
            "temporary_password ":["The temporary password field is required."]
            };
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <ul ng-repeat="(key, value) in errors">
    <li ng-repeat="error in value">{{ error }}</li>
  </ul>
</div>

答案 1 :(得分:0)

您可以像下面这样简单地打印它们:-

<ul ng-repeat="error in errors">
  <li>{{error[0]}}</li>
</ul>