1

Given a json of array of objects, how could one display it using a ng-repeat dynamically? error key is static / always remain the same. Just the values of error changes. So if I have the password field errored-out then I'll have password key instead on the 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 - ]
5
  • Your JSON is not valid. Commented Mar 20, 2017 at 3:27
  • How come it is not a valid JSON? Commented Mar 20, 2017 at 3:28
  • Just try it with a online validator. Commented Mar 20, 2017 at 3:29
  • Try it again. There are 2 json there. Not one. Commented Mar 20, 2017 at 3:30
  • That's right then. Commented Mar 20, 2017 at 3:31

4 Answers 4

2

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>

Sign up to request clarification or add additional context in comments.

3 Comments

Seems weird that it is returning a string instead of an array. Something like this {"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."}}}
And having this error Error: ngRepeat:dupes Duplicate Key in Repeater using the track by $index displays each letter as an array though.
it sounds like you are looping through a string instead of valid object. You may need to parse the JSON string. use JSON.parse(YOUR_ERROR_RESPONSE)
0

Make sure you are using the ng-repeat on the right object. You have:

ng-repeat="message in errormessage"

and the content of errormessage seems to be "[]". See http://jsfiddle.net/hgk6vsqv/

1 Comment

Better to explain what is needed to make it work, than to set up a demo of something not working. They already have a non-working version
0

Some observations :

  • Given a json of array of objects. Your JSON is not an array of Objects.
  • Try this ng-repeat="message in errormessage.error" instead of ng-repeat="message in errormessage"

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>

Comments

0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.