我需要使用ng-repeat在角度js中打印数组值?

时间:2017-01-23 09:01:35

标签: angularjs

我的数组值如 [0,1,5,10] 。 我需要输出像这样 的 0 1 b b b 五 b b b b 10

对于数组中的空值,我想打印为 b 。我怎么能用ng-repeat做这件事,我不想要它想要动态的静态编码。

我的代码是这样的:



var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) { 
        $scope.q = [0,1,5,10];
        });

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div  ng-app="myApp" ng-controller="customersCtrl">
  <div ng-repeat="i in q">{{i}}</div>
</div>
&#13;
&#13;
&#13; 还有一件事我不想使用array.push()方法创建一个新数组。 请提前帮助我。

3 个答案:

答案 0 :(得分:1)

&#13;
&#13;
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) { 
        $scope.q = [0,1,5,10];
        $scope.temp = function(){
          var answer = [];
          for(var i =  $scope.q[0]; i <= $scope.q[$scope.q.length-1]; i++)
              answer.push($scope.q.indexOf(i) == -1 ? 'b' : i);
          return answer;
        }
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div  ng-app="myApp" ng-controller="customersCtrl">
  <div ng-repeat="i in temp() track by $index">{{i}}</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $scope.q = [0, 1, 5, 10];
  $scope.getNumber = function(num) {
    if (num > 0) return new Array(num);
  };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<div ng-app="myApp" ng-controller="customersCtrl">
  <div ng-repeat="i in q">
    <div ng-repeat="j in getNumber(q[$index] - q[$index-1]-1) track by $index">b</div>
    {{i}}
  </div>
</div>

答案 2 :(得分:0)

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) { 
        $scope.q = [0,1,5,10];
		$scope.empties = function(index) {
			return index === 0 ? [] : new Array($scope.q[index] - $scope.q[index - 1] - 1);					
		}				
        });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>    
<div  ng-app="myApp" ng-controller="customersCtrl">			
	<div ng-repeat = "i in q">
		<div ng-repeat = "empty in empties($index) track by $index">b</div>
		<div>{{i}}</div>
	</div>
</div>
</div>