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?
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.
You can consider using transclusion inside a custom directive, to achieve the behavior you are looking for without using ng-repeat.
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.
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 .
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With