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

Commit 562c4e4

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 a0ae07b commit 562c4e4

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
@@ -1425,7 +1425,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14251425

14261426
isolateScope = scope.$new(true);
14271427

1428-
if (templateDirective && (templateDirective === newIsolateScopeDirective.$$originalDirective)) {
1428+
if (templateDirective && (templateDirective === newIsolateScopeDirective ||
1429+
templateDirective === newIsolateScopeDirective.$$originalDirective)) {
14291430
$linkNode.data('$isolateScope', isolateScope) ;
14301431
} else {
14311432
$linkNode.data('$isolateScopeNoTemplate', isolateScope);

test/ng/compileSpec.js

+53
Original file line numberDiff line numberDiff line change
@@ -1797,6 +1797,19 @@ describe('$compile', function() {
17971797
}
17981798
};
17991799
});
1800+
directive('stscope' + uppercase(name), function(log) {
1801+
return {
1802+
scope: true,
1803+
restrict: 'CA',
1804+
template: '<span></span>',
1805+
compile: function() {
1806+
return function (scope, element) {
1807+
log(scope.$id);
1808+
expect(element.data('$scope')).toBe(scope);
1809+
};
1810+
}
1811+
};
1812+
});
18001813
directive('trscope' + uppercase(name), function(log) {
18011814
return {
18021815
scope: true,
@@ -1825,6 +1838,20 @@ describe('$compile', function() {
18251838
}
18261839
};
18271840
});
1841+
directive('stiscope' + uppercase(name), function(log) {
1842+
return {
1843+
scope: {},
1844+
restrict: 'CA',
1845+
template: '<span></span>',
1846+
compile: function() {
1847+
return function (scope, element) {
1848+
iscope = scope;
1849+
log(scope.$id);
1850+
expect(element.data('$isolateScope')).toBe(scope);
1851+
};
1852+
}
1853+
};
1854+
});
18281855
});
18291856
directive('log', function(log) {
18301857
return {
@@ -1999,6 +2026,13 @@ describe('$compile', function() {
19992026
expect(element.find('a').scope().$parent).toBe($rootScope);
20002027
})
20012028
);
2029+
2030+
it('should return the new scope for children in the directive sync template', inject(
2031+
function($rootScope, $compile) {
2032+
element = $compile('<div stscope></div>')($rootScope);
2033+
expect(element.find('span').scope().$parent).toBe($rootScope);
2034+
})
2035+
);
20022036
});
20032037

20042038

@@ -2041,6 +2075,14 @@ describe('$compile', function() {
20412075
expect(element.isolateScope()).not.toBe($rootScope);
20422076
})
20432077
);
2078+
2079+
it('should return the isolate scope for children in directive sync template', inject(
2080+
function($rootScope, $compile) {
2081+
element = $compile('<div stiscope></div>')($rootScope);
2082+
expect(element.find('span').scope()).toBe(element.isolateScope());
2083+
expect(element.isolateScope()).not.toBe($rootScope);
2084+
})
2085+
);
20442086
});
20452087

20462088

@@ -2070,6 +2112,17 @@ describe('$compile', function() {
20702112
expect(child.scope()).toBe(directiveElement.isolateScope());
20712113
})
20722114
);
2115+
2116+
it('should return the isolate scope for child elements in directive sync template', inject(
2117+
function($rootScope, $compile) {
2118+
var directiveElement, child;
2119+
element = $compile('<div><a ng-if="true" stiscope></a></div>')($rootScope);
2120+
$rootScope.$apply();
2121+
directiveElement = element.find('a');
2122+
child = directiveElement.find('span');
2123+
expect(child.scope()).toBe(directiveElement.isolateScope());
2124+
})
2125+
);
20732126
});
20742127
});
20752128
});

0 commit comments

Comments
 (0)