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

Commit 5319621

Browse files
shahatapetebacondarwin
authored andcommitted
fix($compile): set $isolateScope correctly for sync template directives
All isolated scope directives that do not have `templateUrl` were marked as `$isolateScopeNoTemplate` even if they did have a `template` attribute. This caused `jqLite#scope()` to return the wrong value for child elements within the directive's template. Closes #6942
1 parent 32aa491 commit 5319621

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

src/ng/compile.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1408,7 +1408,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14081408

14091409
isolateScope = scope.$new(true);
14101410

1411-
if (templateDirective && (templateDirective === newIsolateScopeDirective.$$originalDirective)) {
1411+
if (templateDirective && (templateDirective === newIsolateScopeDirective ||
1412+
templateDirective === newIsolateScopeDirective.$$originalDirective)) {
14121413
$linkNode.data('$isolateScope', isolateScope) ;
14131414
} else {
14141415
$linkNode.data('$isolateScopeNoTemplate', isolateScope);

test/ng/compileSpec.js

+53
Original file line numberDiff line numberDiff line change
@@ -1681,6 +1681,19 @@ describe('$compile', function() {
16811681
}
16821682
};
16831683
});
1684+
directive('stscope' + uppercase(name), function(log) {
1685+
return {
1686+
scope: true,
1687+
restrict: 'CA',
1688+
template: '<span></span>',
1689+
compile: function() {
1690+
return function (scope, element) {
1691+
log(scope.$id);
1692+
expect(element.data('$scope')).toBe(scope);
1693+
};
1694+
}
1695+
};
1696+
});
16841697
directive('trscope' + uppercase(name), function(log) {
16851698
return {
16861699
scope: true,
@@ -1709,6 +1722,20 @@ describe('$compile', function() {
17091722
}
17101723
};
17111724
});
1725+
directive('stiscope' + uppercase(name), function(log) {
1726+
return {
1727+
scope: {},
1728+
restrict: 'CA',
1729+
template: '<span></span>',
1730+
compile: function() {
1731+
return function (scope, element) {
1732+
iscope = scope;
1733+
log(scope.$id);
1734+
expect(element.data('$isolateScope')).toBe(scope);
1735+
};
1736+
}
1737+
};
1738+
});
17121739
});
17131740
directive('log', function(log) {
17141741
return {
@@ -1883,6 +1910,13 @@ describe('$compile', function() {
18831910
expect(element.find('a').scope().$parent).toBe($rootScope);
18841911
})
18851912
);
1913+
1914+
it('should return the new scope for children in the directive sync template', inject(
1915+
function($rootScope, $compile) {
1916+
element = $compile('<div stscope></div>')($rootScope);
1917+
expect(element.find('span').scope().$parent).toBe($rootScope);
1918+
})
1919+
);
18861920
});
18871921

18881922

@@ -1925,6 +1959,14 @@ describe('$compile', function() {
19251959
expect(element.isolateScope()).not.toBe($rootScope);
19261960
})
19271961
);
1962+
1963+
it('should return the isolate scope for children in directive sync template', inject(
1964+
function($rootScope, $compile) {
1965+
element = $compile('<div stiscope></div>')($rootScope);
1966+
expect(element.find('span').scope()).toBe(element.isolateScope());
1967+
expect(element.isolateScope()).not.toBe($rootScope);
1968+
})
1969+
);
19281970
});
19291971

19301972

@@ -1954,6 +1996,17 @@ describe('$compile', function() {
19541996
expect(child.scope()).toBe(directiveElement.isolateScope());
19551997
})
19561998
);
1999+
2000+
it('should return the isolate scope for child elements in directive sync template', inject(
2001+
function($rootScope, $compile) {
2002+
var directiveElement, child;
2003+
element = $compile('<div><a ng-if="true" stiscope></a></div>')($rootScope);
2004+
$rootScope.$apply();
2005+
directiveElement = element.find('a');
2006+
child = directiveElement.find('span');
2007+
expect(child.scope()).toBe(directiveElement.isolateScope());
2008+
})
2009+
);
19572010
});
19582011
});
19592012
});

0 commit comments

Comments
 (0)