Skip to content

Commit 07ef6b6

Browse files
committed
feat(ngRepeat): New exposed properties
Added new exposed properties to ngRepeat: - string to identify the key (if applicable) - string to identify the variable This allows you to access the key/var from scope inside a directive without needing to know the exact name it was given.
1 parent c0cb9f8 commit 07ef6b6

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/ng/directive/ngRepeat.js

100644100755
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* * `$first` – `{boolean}` – true if the repeated element is first in the iterator.
1616
* * `$middle` – `{boolean}` – true if the repeated element is between the first and last in the iterator.
1717
* * `$last` – `{boolean}` – true if the repeated element is last in the iterator.
18+
* * `$keyIdentifier` – `{string}` – Identifier for the key in the (key,value) pair in the expression (if applicable)
19+
* * `$varIdentifier` – `{string}` – Identifier for the variable in the expression
1820
*
1921
*
2022
* @element ANY
@@ -151,6 +153,8 @@ var ngRepeatDirective = ngDirective({
151153
childScope.$first = (index === 0);
152154
childScope.$last = (index === (collectionLength - 1));
153155
childScope.$middle = !(childScope.$first || childScope.$last);
156+
childScope.$varIdentifier = valueIdent;
157+
if (keyIdent) childScope.$keyIdentifier = keyIdent;
154158

155159
if (!last) {
156160
linker(childScope, function(clone){

test/ng/directive/ngRepeatSpec.js

100644100755
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,29 @@ describe('ngRepeat', function() {
215215
});
216216

217217

218+
it('should expose identifier as $varIdentifier when iterating over arrays',
219+
function() {
220+
element = $compile(
221+
'<ul>' +
222+
'<li ng-repeat="item in items">{{item}}:{{$varIdentifier}}:{{$keyIdentifier}}|</li>' +
223+
'</ul>')(scope);
224+
scope.items = ['misko', 'shyam', 'frodo'];
225+
scope.$digest();
226+
expect(element.text()).toEqual('misko:item:|shyam:item:|frodo:item:|');
227+
});
228+
229+
230+
it('should expose identifiers as $keyIdentifier, $varIdentifier when iterating over objects', function() {
231+
element = $compile(
232+
'<ul>' +
233+
'<li ng-repeat="(key, val) in items">{{key}}:{{val}}:{{$varIdentifier}}:{{$keyIdentifier}}|</li>' +
234+
'</ul>')(scope);
235+
scope.items = {'misko':'m', 'shyam':'s', 'frodo':'f'};
236+
scope.$digest();
237+
expect(element.text()).toEqual('frodo:f:val:key|misko:m:val:key|shyam:s:val:key|');
238+
});
239+
240+
218241
it('should expose iterator offset as $index when iterating over arrays',
219242
function() {
220243
element = $compile(

0 commit comments

Comments
 (0)