2017-07-04 25 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>

相關問題