2

I'm trying to figure out on how to iterate the array using ngrepeat

If I have a array of data like below

{ "People": [{ "name": "Andrew Amernante", "rating": 3, }, { "name": "Frank Wang", "rating": 5, }, { "name": "Chang Wang", "rating": 5, } ] } 

In Controller, I have these code snippets.

app.controller('mainController', function($scope, $http) { $http.get('people.json'). then(function onSuccess(response) { console.log(response); $scope.peoples = response.data; }). catch(function onError(response) { console.log(response); }); }); 

And, I wanted to iterate the array and display the three name in list.

<ul class="nav"> <li ng-repeat="obj.peoples track by $index"> <a href="#/">{{obj.name}}</a> </li> </ul> 

But, I cannot able to get the names, any idea on this?

FYI - I'm using Angular 1.6.5 version here.

Plunkr Code here

3 Answers 3

4

You need to fix your HTML code.

Instead of ng-repeat="obj.peoples track by $index" it should be ng-repeat="obj in peoples.People track by $index"

See below demo.

angular.module('app', []) .controller('mainController', function($scope, $http) { // mocked, equivalente to `$scope.peoples = response.data` $scope.peoples = { "People": [{ "name": "Andrew Amernante", "rating": 3, }, { "name": "Frank Wang", "rating": 5, }, { "name": "Chang Wang", "rating": 5, } ] }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app='app' ng-controller='mainController'> <ul class="nav"> <li ng-repeat="obj in peoples.People track by $index"> <a href="#/">{{obj.name}}</a> </li> </ul> </div>

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

6 Comments

any idea on how can I use single page application here? if we have different set of dynamic list
@UI_Dev you're already using a single page app here (using Angular), aren't you? What do you mean with "if we have different set of dynamic list"?
plnkr.co/edit/iheQU9mYXZwyHVlI6MeP?p=preview here is my updated plunkr, i cannot able to switch between templates
@UI_Dev I think you're trying to use ng-view incorrectly. Maybe you want to read the ngView documentation in order to learn how to use it properly?
@UI_Dev this is a whole new issue you might want to ask in a separate question. This way more people here on SO can help you since this thread in my answer is only being seen by you and me.
|
1

it is peoples in your script.js and you are using obj in html

$http.get('people.json'). then(function onSuccess(response) { console.log(response); $scope.peoples = response.data; }). 

change your html to the below code,

<li class="active" ng-repeat="item in peoples.People"> <a href="#/">{{item.name}}<span class="glyphicon glyphicon-play"></span></a> </li> 

4 Comments

<li class="active" ng-repeat="item in peoples.People track by $index"> this will also work
@UI_Dev , thanks for the reply, is this the correct answer ?.
In this case, how can I use single page application here? if we have different set of dynamic list
we can use this iteration if we have same key in all the dynamic list
-1

Two things, in javscript

 $scope.peoples = response.data.People; 

and in ng-repeat it should be

ng-repeat="people in peoples track by $index" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.