26

Using ng-repeat, how would I loop through the following:

var  messages : [
      {text:"Standard Message"},
      {text:"Success Message!", type:"success"},
      {text:"Alert Message!", type : "alert"},
      {text:"secondary message...", type : "secondary"}
    ]

I've tried:

<p ng-repeat="message in messages">{{message}}</p> 

and it doesn't seem to work, how would I do this?

2
  • jsfiddle.net/aurDq .. or read the front page tutorial Commented Jun 26, 2013 at 18:02
  • 8
    I am not sure why this question has been marked off-topic as the OP has shown 'what he is trying to do' by showing the html markup, and the accepted answer shows what he was missing. Although primitive, I happen to see such questions all the time on SO, and rightfully so, as the OP has missed something very tiny on which he has spent a lot of time, and other people are there to point out the tiny mistake. This question is not vague imho to be marked off-topic. Commented Jan 15, 2017 at 15:19

1 Answer 1

38

You need to insert your messages array into the $scope:

$scope.messages = [
      {text:"Standard Message"},
      {text:"Success Message!", type:"success"},
      {text:"Alert Message!", type : "alert"},
      {text:"secondary message...", type : "secondary"}
    ]

and then use it as following:

<p ng-repeat="message in messages">{{message.text}}</p>
Sign up to request clarification or add additional context in comments.

3 Comments

Weird, now {{message}} is even working. I was looking to dump out the object. Oh well, works now, not sure what was wrong..
@ChrisChevalier I don't understand your question, can you elaborate?
He doesn't understand for X in Y. @DorCohen, it is looping over the array. For each message in $scope.messages - it isn't singular/plural. You can use any variable (although it is a quite common convention). For instance, it could have been written <p ng-repeat="x in messages">{{x.text}}</p>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.