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

Commit 705f4bb

Browse files
committed
fix($compile): attach scope to the directive element when templateUrl and replace=true
We forgot to reattach the scope to the replacement element. This affected only directives that had templateUrl and replace:true properties. Reported on the mailing list: https://groups.google.com/forum/?fromgroups#!topic/angular/zwjLr1msS2Y http://jsfiddle.net/lukebayes/g9Sh9/
1 parent bd530e2 commit 705f4bb

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

src/ng/compile.js

+12-1
Original file line numberDiff line numberDiff line change
@@ -852,13 +852,24 @@ function $CompileProvider($provide) {
852852
linkRootElement = linkQueue.pop(),
853853
cLinkNode = linkQueue.pop(),
854854
scope = linkQueue.pop(),
855-
node = templateNode;
855+
node = templateNode,
856+
cLinkNodeJq = jqLite(cLinkNode);
856857

857858
if (cLinkNode !== originalWidgetNode) {
858859
// it was cloned therefore we have to clone as well.
859860
node = JQLiteClone(templateNode);
860861
replaceWith(linkRootElement, jqLite(cLinkNode), node);
861862
}
863+
864+
if (replace) {
865+
if (cLinkNodeJq.data('$scope')) {
866+
// if the original element before replacement had a new scope, the replacement should
867+
// get it as well
868+
jqLite(node).data('$scope', scope);
869+
}
870+
dealoc(cLinkNodeJq);
871+
}
872+
862873
afterWidgetLinkFn(function() {
863874
beforeWidgetLinkFn(afterWidgetChildrenLinkFn, scope, node, rootElement, controller);
864875
}, scope, node, rootElement, controller);

test/ng/compileSpec.js

+54
Original file line numberDiff line numberDiff line change
@@ -991,6 +991,33 @@ describe('$compile', function() {
991991
}
992992
};
993993
});
994+
$compileProvider.directive('tscope' + uppercase(name), function(log) {
995+
return {
996+
scope: true,
997+
restrict: 'CA',
998+
templateUrl: 'tscope.html',
999+
compile: function() {
1000+
return function (scope, element) {
1001+
log(scope.$id);
1002+
expect(element.data('$scope')).toBe(scope);
1003+
};
1004+
}
1005+
};
1006+
});
1007+
$compileProvider.directive('trscope' + uppercase(name), function(log) {
1008+
return {
1009+
scope: true,
1010+
replace: true,
1011+
restrict: 'CA',
1012+
templateUrl: 'trscope.html',
1013+
compile: function() {
1014+
return function (scope, element) {
1015+
log(scope.$id);
1016+
expect(element.data('$scope')).toBe(scope);
1017+
};
1018+
}
1019+
};
1020+
});
9941021
$compileProvider.directive('tiscope' + uppercase(name), function(log) {
9951022
return {
9961023
scope: {},
@@ -1034,6 +1061,33 @@ describe('$compile', function() {
10341061
}));
10351062

10361063

1064+
it('should allow creation of new scopes for directives with templates', inject(
1065+
function($rootScope, $compile, log, $httpBackend) {
1066+
$httpBackend.expect('GET', 'tscope.html').respond('<a log>{{name}}; scopeId: {{$id}}</a>');
1067+
element = $compile('<div><span tscope></span></div>')($rootScope);
1068+
$httpBackend.flush();
1069+
expect(log).toEqual('LOG; log-002-001; 002');
1070+
$rootScope.name = 'Jozo';
1071+
$rootScope.$apply();
1072+
expect(element.text()).toBe('Jozo; scopeId: 002');
1073+
expect(element.find('span').scope().$id).toBe('002');
1074+
}));
1075+
1076+
1077+
it('should allow creation of new scopes for replace directives with templates', inject(
1078+
function($rootScope, $compile, log, $httpBackend) {
1079+
$httpBackend.expect('GET', 'trscope.html').
1080+
respond('<p><a log>{{name}}; scopeId: {{$id}}</a></p>');
1081+
element = $compile('<div><span trscope></span></div>')($rootScope);
1082+
$httpBackend.flush();
1083+
expect(log).toEqual('LOG; log-002-001; 002');
1084+
$rootScope.name = 'Jozo';
1085+
$rootScope.$apply();
1086+
expect(element.text()).toBe('Jozo; scopeId: 002');
1087+
expect(element.find('a').scope().$id).toBe('002');
1088+
}));
1089+
1090+
10371091
it('should allow creation of new isolated scopes for directives with templates', inject(
10381092
function($rootScope, $compile, log, $httpBackend) {
10391093
$httpBackend.expect('GET', 'tiscope.html').respond('<a log></a>');

0 commit comments

Comments
 (0)