问题描述
Angular 确实为在其 HTML 指令中使用数字的 for 循环提供了一些支持:
做点什么
但是如果您的范围变量包含一个具有动态数字的范围,那么您每次都需要创建一个空数组.
在控制器中
var range = [];for(var i=0;i
在 HTML 中
做点什么
这行得通,但这是不必要的,因为我们根本不会在循环中使用范围数组.有谁知道为最小值/最大值设置范围或常规?
类似于:
做点什么
过滤器定义为:
var myApp = angular.module('myApp', []);myApp.filter('range', function() {返回函数(输入,总计){总计 = parseInt(total);for (var i=0; i
像这样使用重复:
做点什么
Angular does provide some support for a for loop using numbers within its HTML directives:
<div data-ng-repeat="i in [1,2,3,4,5]">
do something
</div>
But if your scope variable includes a range that has a dynamic number then you will need to create an empty array each time.
In the controller
var range = [];
for(var i=0;i<total;i++) {
range.push(i);
}
$scope.range = range;
In the HTML
<div data-ng-repeat="i in range">
do something
</div>
This works, but it is unnecessary since we won't be using the range array at all within the loop. Does anyone know of setting a range or a regular for min/max value?
Something like:
<div data-ng-repeat="i in 1 .. 100">
do something
</div>
I tweaked this answer a bit and came up with this fiddle.
Filter defined as:
var myApp = angular.module('myApp', []);
myApp.filter('range', function() {
return function(input, total) {
total = parseInt(total);
for (var i=0; i<total; i++) {
input.push(i);
}
return input;
};
});
With the repeat used like this:
<div ng-repeat="n in [] | range:100">
do something
</div>
这篇关于AngularJS For 循环与数字 &范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!