How to use ng-repeat loop by count in angularJS

I want to bind a html table row by specific time values ​​using angularJS binding, as shown below:

<table>
    <tr ng-repeat="x in 5">
       <td>abc</td>
    </tr>
</table>

If anyone has a solution, answer as soon as possible. Thanks

+4
source share
4 answers

You can use such a constructor (code in js is not required):

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

app.controller('myCtrl', function($scope) {

});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body>

  <div ng-app="myApp" ng-controller="myCtrl">
    <table>
      <tr ng-repeat="n in [].constructor(10)  track by $index">
        <td>abc</td>
      </tr>
    </table>
  </div>

</body>

</html>
Run codeHide result
+4
source

Pass the function number and generate an array according to it.

Demo

var app = angular.module('myapp',[]);
app.controller('ctrl',function($scope){
     $scope.getNumber = 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">
    <div ng-controller="ctrl">
      <table>
       <tr ng-repeat="x in getNumber(5) track by $index">
        <td>abc</td>
       </tr>
      </table>        
    </div>
</div>
Run codeHide result
+4
source

:

: {{(data|filter:query).length}}

: {{data.length}}

{{data.length}} -

{{(data|filter:query).length}} -

0
source

Sol 1

<table>
    <tr ng-repeat="x in [1,2,3,4,5]">
       <td>abc</td>
    </tr>
</table>

Sol 2

or create an empty array at the end and use it here, as in laravel

$scope.newarr = new Array(5);

In angular

<table>
    <tr ng-repeat="x in newarr track by $index">
       <td>abc</td>
    </tr>
</table>
0
source

Source: https://habr.com/ru/post/1682202/


All Articles