0

I am new to angularJS.

I want to iterate for loop.

Here is my try,

totalDays = 4;
<tr ng-repeat="key in totalDays">
    ...
</tr>

But i can't able to loop.

Thanks in advance.

6
  • Can you post your controller ??? Commented Nov 6, 2017 at 8:04
  • w3schools.com/angular/ng_ng-repeat.asp Commented Nov 6, 2017 at 8:04
  • Thanks @Edison. But in this example loop through array. But i have only single integer value. Commented Nov 6, 2017 at 8:06
  • @JaydeepMor can you show the value of` totalDays` Commented Nov 6, 2017 at 8:11
  • kindly post your js code in your controller Commented Nov 6, 2017 at 8:12

1 Answer 1

4

You need to pass the number to a function and generate an array as follows,

 $scope.getDays = function(num) {
    return new Array(num);   
 }

DEMO

 var app = angular.module('myapp',[]);
 app.controller('personController',function($scope){
  $scope.totalDays = 4;
  $scope.getDays = function(num) {
    return new Array(num);   
 }
 });
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app = "myapp" ng-controller = "personController">
  <ul>
    <li ng-repeat="i in getDays(totalDays) track by $index"><span>{{$index+1}}</span></li>
</ul>
  </div>

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

1 Comment

Working well. Thank you very much !. @Sajeetharan

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.