如何重复AngularJS元素呢? [英] How to repeat elements in AngularJS?

查看:75
本文介绍了如何重复AngularJS元素呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以使用 NG-重复来遍历集合。我如何遍历范围。例如,是这样的:

 < D​​IV CLASS ='aoeu'NG重复='计数范围(1,5)'>
  < D​​IV ID ='{{数}}'>< / DIV>
< / DIV>


解决方案

有是在Javascript中没有范围()函数,但是你可以创建一个返回值的数组适合您需要的功能,或使用JS库这种效果。我会用 underscore.js ,很干脆:

 < D​​IV CLASS ='aoeu'NG重复='计数_.range(1,5)'>
  < D​​IV ID ='{{数}}'>< / DIV>
< / DIV>

当然,你能在你的角度应用使用下划线(在指令,等等),你需要把它列入你的作用域。在项目中包含underscore.js,并在主控制器做 $ rootScope._ = _

这个方式,在任何角度控制器或指令识别$ scope._(引用下划线库),和上面的代码将工作! (如果你永远都需要的所有是创造任意的范围,可以使自己的范围内功能 - 通过'让'我的意思是复制任何可用的百万个可利用的例子.. <一href=\"http://yesudeep.word$p$pss.com/2009/07/25/implementing-a-pythonic-range-function-in-javascript-2/\">Here's 之一)

I know I can use ng-repeat to iterate over collections. How do I iterate over a range. For example, something like:

<div class='aoeu' ng-repeat='count in range(1,5)'>
  <div id='{{count}}'></div>
</div>

解决方案

There is no range() function in Javascript, but you can create a function that returns an array of values that suits your needs, or use a JS library for that effect. I would use underscore.js, very simply:

<div class='aoeu' ng-repeat='count in _.range(1,5)'>
  <div id='{{count}}'></div>
</div>

Of course, for you to be able to use underscore in your angular app (in directives, and such), you need to include it in your scopes. Include underscore.js in your project, and in your main controller do $rootScope._ = _

This way, any controller or directive in angular recognizes a $scope._ (that references the underscore library), and the above snippet will work! (if all you will ever need is to create arbitrary ranges, you can make the range function yourself - and by 'make' I mean copying any of the million available examples available.. Here's one)

这篇关于如何重复AngularJS元素呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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