AngularJS For循环与数字与放大器;范围 [英] AngularJS For Loop with Numbers & Ranges

查看:212
本文介绍了AngularJS For循环与数字与放大器;范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

角确实提供了使用它的HTML指令内的数字环路一些支持:

 < D​​IV数据-NG-重复=我在[1,2,3,4,5]>
  做一点事
< / DIV>

但是如果你的范围的变量包括具有动态号码,然后一个范围,你需要创建一个空数组各一次。

在控制器

  VAR范围= [];
对于(VAR I = 0; I<全;我++){
  range.push(ⅰ);
}
$ scope.range =范围;

在HTML

 < D​​IV数据-NG-重复=我在范围内>
  做一点事
< / DIV>

这个工作,但它是不必要的,因为我们不使用范围阵列在所有的环内。有谁知道设置范围或最小值/最大值正规的?

是这样的:

 < D​​IV数据-NG-重复=我在1 .. 100>
  做一点事
< / DIV>


解决方案

我调整这个答案了一下,想出了与这个小提琴

定义过滤器:

  VAR对myApp = angular.module('对myApp',[]);
myApp.filter('范围',函数(){
  返回功能(输入,总){
    总= parseInt函数(总);    对于(VAR I = 0; I<全;我++){
      input.push(ⅰ);
    }    返回输入;
  };
});

通过使用这样的重复:

 < D​​IV NG重复=N在[] |范围:100>
  做一点事
< / DIV>

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循环与数字与放大器;范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆