ディレクトリ 検索
AngularJS API Reference auto auto/service auto/service/$injector auto/service/$provide ng ng/directive ng/directive/a ng/directive/form ng/directive/input ng/directive/input[checkbox] ng/directive/input[date] ng/directive/input[dateTimeLocal] ng/directive/input[email] ng/directive/input[month] ng/directive/input[number] ng/directive/input[radio] ng/directive/input[text] ng/directive/input[time] ng/directive/input[url] ng/directive/input[week] ng/directive/ngApp ng/directive/ngBind ng/directive/ngBindHtml ng/directive/ngBindTemplate ng/directive/ngBlur ng/directive/ngChange ng/directive/ngChecked ng/directive/ngClass ng/directive/ngClassEven ng/directive/ngClassOdd ng/directive/ngClick ng/directive/ngCloak ng/directive/ngController ng/directive/ngCopy ng/directive/ngCsp ng/directive/ngCut ng/directive/ngDblclick ng/directive/ngDisabled ng/directive/ngFocus ng/directive/ngForm ng/directive/ngHide ng/directive/ngHref ng/directive/ngIf ng/directive/ngInclude ng/directive/ngInit ng/directive/ngKeydown ng/directive/ngKeypress ng/directive/ngKeyup ng/directive/ngList ng/directive/ngModel ng/directive/ngModelOptions ng/directive/ngMousedown ng/directive/ngMouseenter ng/directive/ngMouseleave ng/directive/ngMousemove ng/directive/ngMouseover ng/directive/ngMouseup ng/directive/ngNonBindable ng/directive/ngOpen ng/directive/ngPaste ng/directive/ngPluralize ng/directive/ngReadonly ng/directive/ngRepeat ng/directive/ngSelected ng/directive/ngShow ng/directive/ngSrc ng/directive/ngSrcset ng/directive/ngStyle ng/directive/ngSubmit ng/directive/ngSwitch ng/directive/ngTransclude ng/directive/ngValue ng/directive/script ng/directive/select ng/directive/textarea ng/filter ng/filter/currency ng/filter/date ng/filter/filter ng/filter/json ng/filter/limitTo ng/filter/lowercase ng/filter/number ng/filter/orderBy ng/filter/uppercase ng/function ng/function/angular.bind ng/function/angular.bootstrap ng/function/angular.copy ng/function/angular.element ng/function/angular.equals ng/function/angular.extend ng/function/angular.forEach ng/function/angular.fromJson ng/function/angular.identity ng/function/angular.injector ng/function/angular.isArray ng/function/angular.isDate ng/function/angular.isDefined ng/function/angular.isElement ng/function/angular.isFunction ng/function/angular.isNumber ng/function/angular.isObject ng/function/angular.isString ng/function/angular.isUndefined ng/function/angular.lowercase ng/function/angular.module ng/function/angular.noop ng/function/angular.toJson ng/function/angular.uppercase ng/object ng/object/angular.version ng/provider ng/provider/$animateProvider ng/provider/$compileProvider ng/provider/$controllerProvider ng/provider/$filterProvider ng/provider/$httpProvider ng/provider/$interpolateProvider ng/provider/$locationProvider ng/provider/$logProvider ng/provider/$parseProvider ng/provider/$rootScopeProvider ng/provider/$sceDelegateProvider ng/provider/$sceProvider ng/service ng/service/$anchorScroll ng/service/$animate ng/service/$cacheFactory ng/service/$compile ng/service/$controller ng/service/$document ng/service/$exceptionHandler ng/service/$filter ng/service/$http ng/service/$httpBackend ng/service/$interpolate ng/service/$interval ng/service/$locale ng/service/$location ng/service/$log ng/service/$parse ng/service/$q ng/service/$rootElement ng/service/$rootScope ng/service/$sce ng/service/$sceDelegate ng/service/$templateCache ng/service/$timeout ng/service/$window ng/type ng/type/$cacheFactory.Cache ng/type/$compile.directive.Attributes ng/type/$rootScope.Scope ng/type/angular.Module ng/type/form.FormController ng/type/ngModel.NgModelController ngAnimate ngAnimate/provider ngAnimate/provider/$animateProvider ngAnimate/service ngAnimate/service/$animate ngCookies ngCookies/service ngCookies/service/$cookies ngCookies/service/$cookieStore ngMessages ngMessages/directive ngMessages/directive/ngMessage ngMessages/directive/ngMessages ngMock ngMock/function ngMock/function/angular.mock.dump ngMock/function/angular.mock.inject ngMock/function/angular.mock.module ngMock/object ngMock/object/angular.mock ngMock/provider ngMock/provider/$exceptionHandlerProvider ngMock/service ngMock/service/$exceptionHandler ngMock/service/$httpBackend ngMock/service/$interval ngMock/service/$log ngMock/service/$timeout ngMock/type ngMock/type/angular.mock.TzDate ngMockE2E ngMockE2E/service ngMockE2E/service/$httpBackend ngResource ngResource/service ngResource/service/$resource ngRoute ngRoute/directive ngRoute/directive/ngView ngRoute/provider ngRoute/provider/$routeProvider ngRoute/service ngRoute/service/$route ngRoute/service/$routeParams ngSanitize ngSanitize/filter ngSanitize/filter/linky ngSanitize/service ngSanitize/service/$sanitize ngTouch ngTouch/directive ngTouch/directive/ngClick ngTouch/directive/ngSwipeLeft ngTouch/directive/ngSwipeRight ngTouch/service ngTouch/service/$swipe
テキスト

AngularJS: API: ng/directive/ngRepeat

ngRepeat

  1. - directive in module ng

ngRepeat指令为集合中的每项实例化一个模板。每个模板的实例拥有自己的域,使用循环变量指向当前集合项上,$index指向当前项的索引或键值。

特殊属性应用于每个模板实例的本地域上,包括:

变量 类型 详述
$index number 重复元素的迭代偏移值(0..length-1)
$first boolean 如果是重复元素的第一个迭代项则为true
$middle boolean 如果是在重复元素的第一个和最后一个之间的迭代项则为true
$last boolean 如果是重复元素的最后一个迭代项则为true
$even boolean 如果迭代项的位置$index是偶数则为true(其它为false)
$odd boolean 如果迭代项的位置$index为奇数则为true(其它为false)

这些也可以用ngInit创建别名。这在实例化嵌套ngRepeats时很有用。

指定重复的开始和结束点

为了解决重复元素序列只有一个父元素的情况,ngRepeat (不同于其它ng指令)支持扩展重复范围,通过使用ng-repeat-startng-repeat-end分别定义明确的开始和结束点。ng-repeat-start 指令工作方法类似ng-repeat,但是会重复从标签定义本身位置开始到ng-repeat-end定义位置之间的所有HTML代码。

这个例子演示了它的使用场景:

  <header ng-repeat-start="item in items">
    Header {{ item }}
  </header>
  <div class="body">
    Body {{ item }}
  </div>
  <footer ng-repeat-end>
    Footer {{ item }}
  </footer>

如果上面例子中的items变量的值为['A','B'],则输出结果为:

  <header>
    Header A
  </header>
  <div class="body">
    Body A
  </div>
  <footer>
    Footer A
  </footer>
  <header>
    Header B
  </header>
  <div class="body">
    Body B
  </div>
  <footer>
    Footer B
  </footer>

为 ngRepeat指定开始和结束点也支持AngularJS中的所有其它HTML指令语法(例如data-ng-repeat-start, x-ng-repeat-startng:repeat-start)。

指令信息

  • 这个指令会创建新的作用域(scope)。
  • 这个指令执行优先级为1000.

用法

  • 作为属性使用:
    <ANY
      ng-repeat="">
    ...
    </ANY>

动画

.enter - 当一个新的项添加到列表或条目经过过滤显示时

.leave - 当一个项从列表中删除或当一个项被过滤掉时

.move - 当相邻的一个项被过滤掉引起重新排序,或当条目内容进行重新排序时

点击这里 了解更多关于涉及动画的步骤。

参数

参数 类型 详述
ngRepeat repeat_expression

决定如果枚举集合的表达式。当前支持的格式有:

  • variable in expression – variable是用户定义的循环变量, expression是用于枚举集合的域表达式。

    例如: album in artist.albums.

  • (key, value) in expressionkeyvalue 可以是任何用户定义的标识, expression是用于枚举集合的域表达式。

    例如: (name, age) in {'adam':10, 'amalie':12}.

  • variable in expression track by tracking_expression – 您也可以提供一个可选的跟踪函数,用来关联到DOM元素集合中的对象。如果没有给出跟踪函数,ng-repeat使用标识来关联集合中的元素。对于解决相同键,它会比使用一个跟踪函数产生更多错误。(意思是会有两个不同对象映射到相同的DOM元素,也许不会。) 表达式可以使用过滤器,它会在调用跟踪函数前生效。

    例如: item in items 等同于 item in items track by $id(item). 这意味着,DOM元素会关联到数组中的条目标识上。

    例如: item in items track by $id(item). 内建的$id()函数能够用于为数组内的条目分配一个唯一的$$hashKey属性。这个属性用于作为数组中的条目标识关联到DOM元素的键值。移动数组中的同一个对象同样会在DOM中移动DOM元素。

    例如: item in items track by item.id 是当items来自数据库时的典型模式。在这种情况下,对象的标识不会有问题。两个对象被认为是相等的,只要他们的 id属性是一样的。

    例如: item in items | Filter:searchText track by item.id 是对条目同时使用过滤器和跟踪表达式的模式。

示例

这个例子在域中初始化一个名字列表,并使用ngRepeat显示每个人:

index.html
<div ng-init="friends = [
  {name:'John', age:25, gender:'boy'},
  {name:'Jessie', age:30, gender:'girl'},
  {name:'Johanna', age:28, gender:'girl'},
  {name:'Joy', age:15, gender:'girl'},
  {name:'Mary', age:28, gender:'girl'},
  {name:'Peter', age:95, gender:'boy'},
  {name:'Sebastian', age:50, gender:'boy'},
  {name:'Erika', age:27, gender:'girl'},
  {name:'Patrick', age:40, gender:'boy'},
  {name:'Samantha', age:60, gender:'girl'}
]">
  I have {{friends.length}} friends. They are:
  <input Type="search" ng-model="q" placeholder="filter friends..." />
  <ul class="example-animate-container">
    <li class="animate-repeat" ng-repeat="friend in friends | filter:q">
      [{{$index + 1}}] {{friend.name}} who is {{friend.age}} years old.
    </li>
  </ul>
</div>
animations.css
.示例:-animate-container {
  background:white;
  border:1px solid black;
  list-style:none;
  margin:0;
  padding:0 10px;}

.animate-repeat {
  line-height:40px;
  list-style:none;
  box-sizing:border-box;}

.animate-repeat.ng-move,.animate-repeat.ng-enter,.animate-repeat.ng-leave {
  -webkit-transition:all linear 0.5s;
  transition:all linear 0.5s;}

.animate-repeat.ng-leave.ng-leave-active,.animate-repeat.ng-move,.animate-repeat.ng-enter {
  opacity:0;
  max-height:0;}

.animate-repeat.ng-leave,.animate-repeat.ng-move.ng-move-active,.animate-repeat.ng-enter.ng-enter-active {
  opacity:1;
  max-height:40px;}
protractor.js
var friends = element.all(by.repeater('friend in friends'));

it('should render initial data set', Function() {
  expect(friends.count()).toBe(10);
  expect(friends.get(0).getText()).toEqual('[1] John who is 25 years old.');
  expect(friends.get(1).getText()).toEqual('[2] Jessie who is 30 years old.');
  expect(friends.last().getText()).toEqual('[10] Samantha who is 60 years old.');
  expect(element(by.binding('friends.length')).getText())
      .toMatch("I have 10 friends. They are:");});

 it('should update repeater when filter predicate changes', Function() {
   expect(friends.count()).toBe(10);

   element(by.model('q')).sendKeys('ma');

   expect(friends.count()).toBe(2);
   expect(friends.get(0).getText()).toEqual('[1] Mary who is 28 years old.');
   expect(friends.last().getText()).toEqual('[2] Samantha who is 60 years old.');
 });
前の記事: 次の記事: