Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng-repeat: access key and value for each object in array of objects

I have an array of objects and I am using an ng-repeat to iterate over them, which is easy. The array looks something like this:

$scope.steps = [     {companyName: true},     {businessType: true},     {physicalAddress: true} ]; 

And my ng-repeat attribute looks like:

<div ng-repeat="step in steps"> ... </div> 

In each iteration, step is equal to one of the objects, as expected. Is there anyway to access both the key and the value of the step object? So that I could do something like this:

<div ng-repeat="(stepName, isComplete) in steps"> ... </div> 

Where stepName == 'companyName' and isComplete == true. I've tried doing this exact thing but stepName always just ends up being the index of the step object (which makes sense). I'm just trying to figure out if there is another way to write the ng-repeat syntax so that I can get the key and the value.

Thanks for any ideas/suggestions. Much appreciated.

PS. My current work around is to just do this in my controller:

$scope.stepNames = []; angular.forEach($scope.steps, function(isComplete, stepName){      $scope.stepNames.push({stepName: stepName, isComplete: isComplete}); }); 

And then to iterate over that, but it would be nice to do it all in the view

like image 268
tennisgent Avatar asked Oct 23 '13 14:10

tennisgent


People also ask

How do I get the index of an element in NG-repeat?

Note: The $index variable is used to get the Index of the Row created by ng-repeat directive. Each row of the HTML Table consists of a Button which has been assigned ng-click directive. The $index variable is passed as parameter to the GetRowIndex function.

What does ng-repeat do?

Definition and Usage. The ng-repeat directive repeats a set of HTML, a given number of times. The set of HTML will be repeated once per item in a collection. The collection must be an array or an object.

What is KeyValue in ngFor?

KeyValue pipe released in Angular 6.1 to loop through objects,Maps and arrays. Now by passing KeyValue pipe to *ngFor we can loop through objects key values & maps. Prior to this Angular 6.1 release we cannot iterate directly through objects key values & maps using *ngFor directive.


2 Answers

A repeater inside a repeater

<div ng-repeat="step in steps">     <div ng-repeat="(key, value) in step">         {{key}} : {{value}}     </div> </div> 
like image 63
tymeJV Avatar answered Oct 03 '22 22:10

tymeJV


In fact, your data is not well design. You'd better use something like :

$scope.steps = [     {stepName: "companyName", isComplete: true},     {stepName: "businessType", isComplete: true},     {stepName: "physicalAddress", isComplete: true} ]; 

Then it is easy to do what you want :

<div ng-repeat="step in steps">  Step {{step.stepName}} status : {{step.isComplet}} </div> 

Example: http://jsfiddle.net/rX7ba/

like image 35
GuillaumeA Avatar answered Oct 03 '22 20:10

GuillaumeA