diff options
| author | Igor Minar | 2010-11-10 16:14:47 -0800 |
|---|---|---|
| committer | Igor Minar | 2010-11-10 21:04:44 -0800 |
| commit | c5b2bf083c7044aaf9269ad2cd6469fd14653917 (patch) | |
| tree | 2db5c806e834d33760b8a9d2ae2e154bdc4467f0 /src/directives.js | |
| parent | 0499c4727036446f5c8a5722bbd9c4018dae146f (diff) | |
| download | angular.js-c5b2bf083c7044aaf9269ad2cd6469fd14653917.tar.bz2 | |
Make ng:repeat expose $position.
- $position is a textual representation of the position of
repeated item ('first', 'middle', 'last')
- added specs for $index
Diffstat (limited to 'src/directives.js')
| -rw-r--r-- | src/directives.js | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/src/directives.js b/src/directives.js index 24b85908..50901cbe 100644 --- a/src/directives.js +++ b/src/directives.js @@ -434,6 +434,10 @@ angularWidget("@ng:non-bindable", noop); * scope expression giving the collection to enumerate. * For example: `(name, age) in {'adam':10, 'amalie':12}`. * + * Special properties set on the local scope: + * * {number} $index - iterator offset of the repeated element (0..length-1) + * * {string} $position - position of the repeated element in the iterator ('first', 'middle', 'last') + * * @exampleDescription * This example initializes the scope to a list of names and * than uses `ng:repeat` to display every person. @@ -477,9 +481,24 @@ angularWidget("@ng:repeat", function(expression, element){ var children = [], currentScope = this; this.$onEval(function(){ - var index = 0, childCount = children.length, childScope, lastElement = reference, - collection = this.$tryEval(rhs, reference), is_array = isArray(collection); - for ( var key in collection) { + var index = 0, + childCount = children.length, + lastElement = reference, + collection = this.$tryEval(rhs, reference), + is_array = isArray(collection), + collectionLength = 0, + childScope, + key; + + if (is_array) { + collectionLength = collection.length; + } else { + for (key in collection) + if (collection.hasOwnProperty(key)) + collectionLength++; + } + + for (key in collection) { if (!is_array || collection.hasOwnProperty(key)) { if (index < childCount) { // reuse existing child @@ -493,6 +512,9 @@ angularWidget("@ng:repeat", function(expression, element){ if (keyIdent) childScope[keyIdent] = key; lastElement.after(childScope.$element); childScope.$index = index; + childScope.$position = index == 0 ? + 'first' : + (index == collectionLength - 1 ? 'last' : 'middle'); childScope.$element.attr('ng:repeat-index', index); childScope.$init(); children.push(childScope); |
