AngularJS Tutorial - ng-init and ng-repeat with objects








The following code shows how to use ng-init and ng-repeat with objects.

Example


<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js"></script>
</head><!--   ww  w.  j a  v  a2s  .  c  o  m-->
<body>
    <body ng-app>
    <div ng-init="friends = [{name:'John', age:25}, {name:'Mary', age:28}]">
    I have {{friends.length}} friends. They are:
    <ul>
        <li ng-repeat="friend in friends">
        [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
        </li>
    </ul>
    </div>
</body>
</body>
</html>

The code above is rendered as follows: