0

I want manually run the ng-repeat by hardcode number. I don't want to pass array parameter. I have tried below code but It's does not work!

<h1 ng-repeat="x in 20">{{sumofTwendy(($index}})</h1>

So I have planed to create a dummy array in controller and assign value 20 to length of that array. then I pass the array on `ng-repeat.

   $scope.data=[];
   $scope.data.length=20;
   <h1 ng-repeat="x in data">{{sumofTwendy(($index}})</h1>

this is also does not working :( .


How can we run ng-repeat only by 20 times without passing array values?

let me know the solution if it angular 1 or angular 2?

R. Richards
  • 24,603
  • 10
  • 64
  • 64
Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234

2 Answers2

0

Just create array with length 20:

new Array(20);
Kacper Polak
  • 1,411
  • 15
  • 24
0

Yes you can do by passing variable to a function and generate a new array,

var myApp=angular.module('myApp',[]);

myApp.controller('thecontroller',function($scope){
$scope.getNumber = function(num) {
    return new Array(num);   
}
});
<!DOCTYPE html>
<html>
    <head>
        <title>ng-Messages Service</title>
        <script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>
        <script src='https://code.angularjs.org/1.5.0-rc.0/angular.min.js'></script>     
    </head>
    <body ng-app="myApp">

        <div ng-controller='thecontroller'>
    <ul>
    <li ng-repeat="i in getNumber(20) track by $index"><span>{{$index+1}}</span></li>
</ul>
        </div>
    </body>
</html>
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396