0

I have the following array of objects, which is a server response. I am trying to use ng-repeat to iterate over this array and read each value.

While trying to use in html, array[0].value is working while using ng-repeat, nothing is working.

After a lot of debugging, I am unable to understand how ng-repeat is working for arrays.

Here is my example:

messages array:

[
{"Id":14,"Text":"hii hello","count":750},
{"Id":10009,"Text":"test message","count":6}
]

The following is used in html:

<div  class="my-message" layout="row" layout-align="center center">
       {{messages}} <!-- printing the above array -->
        <div ng-repat="message in messages">
       {{ message.Id}}<!-- printing nothing -->

</div>
{{ messages[0].Id }} <!-- printing 14 !-->
</div>

The array is in scope and it is also seen in this html as the {{ message }} array is printing properly.

Can someone help me understand the working of ng-repeat and where am I missing

3
  • 3
    typo in ng-repat (should be ng-repeat)? Commented Dec 7, 2016 at 9:10
  • 1
    my bad on syntax.thanks Commented Dec 7, 2016 at 9:12
  • expose the array on scope Commented Dec 7, 2016 at 9:56

3 Answers 3

1

In your controller you must have :

$scope.messages = [{"Id":1,...},...]

There is a typo in your view code (ng-repat -> ng-repeat):

<div ng-repat="message in messages">
    {{ message.Id}}
</div>

must be :

<div ng-repeat="message in messages">
    {{ message.Id}}
</div>

It should be fine like this.

Have fun

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

Comments

1

hope this will clears you

<!DOCTYPE html>
<html ng-app="plunker">

  <head>
    <meta charset="utf-8" />
    <title>AngularJS Plunker</title>
    <script>document.write('<base href="' + document.location + '" />');</script>
    <link rel="stylesheet" href="style.css" />
    <script data-require="[email protected]" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    <script src="script.js"></script>
  </head>

  <body ng-controller="MainCtrl">
   <div  class="my-message" layout="row" layout-align="center center">
       {{messages}} <!-- printing the above array -->
        <div ng-repeat="message in messages">
       {{ message.Id}}
         {{ message.Text }} 
         {{ message.count }} 
</div>
</div>
  </body>
<script>
  var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
  $scope.messages =[
{"Id":14,"Text":"hii hello","count":750},
{"Id":10009,"Text":"test message","count":6}
];
 
});
</script>
</html>

Comments

0

The issue is with typo in using "ng-repeat", after correcting it my code worked normally as expected.

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.