我想通过硬代码号手动运行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-repeat
20次呢?
让我知道解是角1还是角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>