提问者:小点点

如何通过硬编码值而不是传递数组[重复]来运行ng-repeat


我想通过硬代码号手动运行ng-repeat。我不想传递array参数。我试过下面的代码,但它不工作!

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

因此,我计划在controller中创建一个虚拟数组,并将值20赋给该数组的长度。然后我将数组传递给`ng-repeat。

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

这也是行不通的:(。

我们如何才能在不传递数组值的情况下只运行ng-repeat20次呢?

让我知道解是角1还是角2?


共2个答案

匿名用户

只需创建长度为20的数组:

new Array(20);

匿名用户

是的,您可以通过将变量传递给函数并生成一个新数组,

null

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>