Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit b87e5fc

Browse files
petebacondarwinvojtajina
authored andcommitted
fix(ngRepeat): ensure that the correct (transcluded) scope is used
1 parent d71df9f commit b87e5fc

File tree

2 files changed

+34
-19
lines changed

2 files changed

+34
-19
lines changed

src/ng/directive/ngRepeat.js

+14-19
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
264264
// lastBlockMap on the next iteration.
265265
nextBlockMap = {},
266266
arrayLength,
267-
childScope,
268267
key, value, // key/value of iteration
269268
trackById,
270269
trackByIdFn,
@@ -273,6 +272,17 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
273272
nextBlockOrder = [],
274273
elementsToRemove;
275274

275+
var updateScope = function(scope, index) {
276+
scope[valueIdentifier] = value;
277+
if (keyIdentifier) scope[keyIdentifier] = key;
278+
scope.$index = index;
279+
scope.$first = (index === 0);
280+
scope.$last = (index === (arrayLength - 1));
281+
scope.$middle = !(scope.$first || scope.$last);
282+
// jshint bitwise: false
283+
scope.$odd = !(scope.$even = (index&1) === 0);
284+
// jshint bitwise: true
285+
};
276286

277287
if (isArrayLike(collection)) {
278288
collectionKeys = collection;
@@ -340,8 +350,6 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
340350
if (block.scope) {
341351
// if we have already seen this object, then we need to reuse the
342352
// associated scope/element
343-
childScope = block.scope;
344-
345353
nextNode = previousNode;
346354
do {
347355
nextNode = nextNode.nextSibling;
@@ -354,32 +362,19 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) {
354362
previousNode = getBlockEnd(block);
355363
} else {
356364
// new item which we don't know about
357-
childScope = $scope.$new();
358-
}
359-
360-
childScope[valueIdentifier] = value;
361-
if (keyIdentifier) childScope[keyIdentifier] = key;
362-
childScope.$index = index;
363-
childScope.$first = (index === 0);
364-
childScope.$last = (index === (arrayLength - 1));
365-
childScope.$middle = !(childScope.$first || childScope.$last);
366-
// jshint bitwise: false
367-
childScope.$odd = !(childScope.$even = (index&1) === 0);
368-
// jshint bitwise: true
369-
370-
if (!block.scope) {
371-
$transclude(childScope, function(clone) {
365+
$transclude(function(clone, scope) {
366+
block.scope = scope;
372367
clone[clone.length++] = document.createComment(' end ngRepeat: ' + expression + ' ');
373368
$animate.enter(clone, null, jqLite(previousNode));
374369
previousNode = clone;
375-
block.scope = childScope;
376370
// Note: We only need the first/last node of the cloned nodes.
377371
// However, we need to keep the reference to the jqlite wrapper as it might be changed later
378372
// by a directive with templateUrl when it's template arrives.
379373
block.clone = clone;
380374
nextBlockMap[block.id] = block;
381375
});
382376
}
377+
updateScope(block.scope, index);
383378
}
384379
lastBlockMap = nextBlockMap;
385380
});

test/ng/directive/ngRepeatSpec.js

+20
Original file line numberDiff line numberDiff line change
@@ -1175,6 +1175,26 @@ describe('ngRepeat and transcludes', function() {
11751175
dealoc(element);
11761176
});
11771177
});
1178+
1179+
1180+
it('should use the correct transcluded scope', function() {
1181+
module(function($compileProvider) {
1182+
$compileProvider.directive('iso', valueFn({
1183+
restrict: 'E',
1184+
transclude: true,
1185+
template: '<div ng-repeat="a in [1]"><div ng-transclude></div></div>',
1186+
scope: {}
1187+
}));
1188+
});
1189+
inject(function($compile, $rootScope) {
1190+
$rootScope.val = 'transcluded content';
1191+
var element = $compile('<iso><span ng-bind="val"></span></iso>')($rootScope);
1192+
$rootScope.$digest();
1193+
expect(trim(element.text())).toEqual('transcluded content');
1194+
dealoc(element);
1195+
});
1196+
});
1197+
11781198
});
11791199

11801200
describe('ngRepeat animations', function() {

0 commit comments

Comments
 (0)