2017-07-04 28 views
0

我想通过硬编码号手动运行ng-repeat。我不想通过array参数。我尝试了下面的代码,但它不起作用!如何通过硬编码值而不是传递数组来运行ng-repeat

<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> 

这也没有工作:(。


我们如何能够只用20次运行ng-repeat没有通过数组值?

让我知道解决方案,如果它角1或角2?

+0

为什么我需要在每种方法中调用一个函数?为什么你标记为重复呢? –

+0

第二个也不适合我的答案。其实清楚地读了我的问题。 –

回答

0

只需创建数组长度为20:

new Array(20); 
+0

但新的Array()不是最佳实践 –

0

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

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>

相关问题