0

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?

1
  • Do you mean you need something like angular.forEach? Commented Nov 4, 2014 at 11:52

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

Comments

1

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'
  ]
}
]

Comments

0

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.