forgottofly
forgottofly

Reputation: 2719

Iterate over an array of integers using ng-repeat

I don't think this question fits in the context of angular.Am having an array of integers var myArray=[1,2,3,4,5].Is it possible to do a ng-repeat and display the integers on myArray

Upvotes: 0

Views: 3617

Answers (4)

NewDirection
NewDirection

Reputation: 114

You can also code as below:

<div ng-repeat="i in [1, 2, 3, 4, 5]">
 {{$index + 1}}
</div>

Upvotes: 1

Pankaj Parkar
Pankaj Parkar

Reputation: 136154

First thing you need place that variable inside scope variable then only you could use it on html.

Controller

angular.module('app', [])
    .controller('Ctrl', function Ctrl($scope) {
    $scope.myArray = [1, 2, 3, 4, 5];
});

Markup

<div ng-app="app" ng-controller="Ctrl">
    <ul>
        <li ng-repeat="item in myArray">{{item}}</li>
    </ul>
</div>

JSFiddle

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691765

Yes:

<div ng-repeat="i in myArray track by $index">
    {{ i }}
</div>

The relevant documentation, BTW, shows an example doing what you want.

And of course, the array must be in the $scope, not just declared as a local variable of your controller.

Upvotes: 3

Dmitri Algazin
Dmitri Algazin

Reputation: 3456

<div ng-repeat="i in myArray">{{i}}</div>``

Upvotes: 3

Related Questions