AngularJS - ng-repeat over array with string index

15,053

Solution 1

You can just use normal syntax for item in an array. Please refer my fiddle

<div ng-app='app' ng-controller='default' ng-init='init()'>
  <div ng-repeat='day in days'>
    <strong>{{day}}</strong><br/>
    <span ng-repeat="item in arr[day]">{{item}} </span> 
  </div>
</div>

https://jsfiddle.net/DoTH/evcv4tu5/3/

Solution 2

Use square brackets to access object/array fields/elements.

<div ng-repeat="day in days">{{day}}
    <span ng-repeat="item in arr[day]">{{item}}</span>
</div>
Share:
15,053
anand patil
Author by

anand patil

Updated on June 09, 2022

Comments

  • anand patil
    anand patil almost 2 years

    How to ng-repeat over array with string index? Please see below snippet of the code-

    Below code is in a controller.

    $scope.days = ["mon", "tue", "wed" .... "sun"];
    $scope.arr = [];
    $scope.arr["mon"] = ["apple","orange"];
    $scope.arr["tue"] = ["blue","swish"];
    .
    .
    .
    $scope.arr["sun"] = ["pineapple","myfruit","carrot"];
    

    Question - How to ng-repeat like something below, is it possible?

    <div ng-repeat="day in days">{{day}}
        <span ng-repeat="item in arr(day)">{{item}}</span> 
        <-- Something like "arr(day)", can it be done in angular -->
    </div>