Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I have an object as follows:

var obj = {
{
  name : 'John',
  address : 'hongkong',
  company : 'hongkong pvt ltd',
  employess : [
  'Ravi', 
  'Kabhi',
  'Abhi Nahi'
  ]
},
{
  name : 'Deo',
  address : 'China',
  company : 'China pvt ltd',
  employess : [
  'Wong', 
  'kong',
  'Lee'
  ]
}
}

I'm accessing the object in the following way, But how do I access the array in loops.

 <div ng-repeat="o in obj">
    <p>{{o.name}}</p>
    <p ng-repeat="e in o.employees">
    <span>{{e}}</span>
   </p>
 </div>

This is how i have been doing in jade but I think angular doesn't recognize it. How can I loop through the array?

share|improve this question
    
Do you mean you need something like angular.forEach? –  Kursad Gulseven Nov 4 '14 at 11:52

3 Answers 3

up vote 1 down vote accepted

Try this : Inside your controller:

  $scope.obj = [
    {
        'name' : 'John',
        'address' : 'hongkong',
        'company' : 'hongkong pvt ltd',
        'employess' : [
        'Ravi',
        'Kabhi',
        'Abhi Nahi'
        ]
    },
    {
        'name' : 'Deo',
        'address' : 'China',
        'company' : 'China pvt ltd',
        'employess' : [
        'Wong',
        'kong',
        'Lee'
    ]
    }
  ];

On your HTML page :

   <div ng-controller="TempCtrl">
       <div ng-repeat="o in obj">
         <p>{{o.name}}</p>
        <hr>
        <p ng-repeat="e in o.employess track by $index">
           <span>{{e}}</span>
        </p>
        <hr>
        <hr>
       </div>
   </div>
share|improve this answer

obj should be array of object.so you have json data problem.json should be:

$scope.obj = [
{
  name : 'John',
  address : 'hongkong',
  company : 'hongkong pvt ltd',
  employess : [
  'Ravi', 
  'Kabhi',
  'Abhi Nahi'
  ]
},
{
  name : 'Deo',
  address : 'China',
  company : 'China pvt ltd',
  employess : [
  'Wong', 
  'kong',
  'Lee'
  ]
}
]
share|improve this answer

Mostly it's a JSON data problem. Otherwise your code would work fine

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.