Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat over an array of objects

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?

like image 872
wesbos Avatar asked Jun 26 '13 17:06

wesbos


People also ask

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object. Note: Each instance of the repetition is given its own scope, which consist of the current item.

What can I use instead of NG-repeat?

You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.

How do I find the NG-repeat index?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

How do you pass an index in NG-repeat?

Each ng-repeat creates a child scope with the passed data, and also adds an additional $index variable in that scope. So what you need to do is reach up to the parent scope, and use that $index .


1 Answers

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> 
like image 77
Dor Cohen Avatar answered Oct 05 '22 19:10

Dor Cohen