0

Hi I'm a bit new to angular and trying to figure out how I could access a json key and iterate and print the values that are contained in an array. In my case below I'm trying to print the names along with their roles.

angular.module('employeeEarnestApp')
  .controller('MainCtrl', function ($scope) {
    $scope.employees = [ { id: 1, name: 'Alex', roles: ['admin','user','CEO'] },
  { id: 2, name: 'John', roles: ['developer','marketing'] },
  { id: 3, name: 'Kim', roles: ['sales','developer','CTO'] } ];
  });

How could I use ng-repeat to print out the roles in the array? I'd imagine it might be something like this below?

 <div class="container">
    <h2>My Employees</h2>
    <p class="form-group" ng-repeat="employee in employees">
     {{employee.id}} - {{employee.name}}
    <p class="form-group" ng-repeat="roles in {{employees.roles}}">
     {{roles}}
   </p>
   </p>
 </div>
1
  • nvm, ng-repeat, which is a directive, automatically evaluates the expression. Commented Mar 11, 2015 at 21:21

1 Answer 1

4

Remove {{}} in ng-repeat, since ng-repeat automatically evaluates the expression, no need for {{}}.

 <div class="container">
    <h2>My Employees</h2>
    <p class="form-group" ng-repeat="employee in employees">
     {{employee.id}} - {{employee.name}}
    <p class="form-group" ng-repeat="role in employee.roles">
     {{role}}
   </p>
   </p>
 </div>

See, http://jsbin.com/hasuyewazi/1/edit

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

3 Comments

the second repeat should be ng-repeat='roles in employee.roles' - NOT employees.roles
Still not working for me - could there be something else I might be missing?
Missed " in employee.roles

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.