mirror of
https://github.com/Hopiu/angular.js.git
synced 2026-05-05 05:34:43 +00:00
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/
This commit is contained in:
parent
bd530e2257
commit
705f4bbf11
2 changed files with 66 additions and 1 deletions
|
|
@ -852,13 +852,24 @@ function $CompileProvider($provide) {
|
||||||
linkRootElement = linkQueue.pop(),
|
linkRootElement = linkQueue.pop(),
|
||||||
cLinkNode = linkQueue.pop(),
|
cLinkNode = linkQueue.pop(),
|
||||||
scope = linkQueue.pop(),
|
scope = linkQueue.pop(),
|
||||||
node = templateNode;
|
node = templateNode,
|
||||||
|
cLinkNodeJq = jqLite(cLinkNode);
|
||||||
|
|
||||||
if (cLinkNode !== originalWidgetNode) {
|
if (cLinkNode !== originalWidgetNode) {
|
||||||
// it was cloned therefore we have to clone as well.
|
// it was cloned therefore we have to clone as well.
|
||||||
node = JQLiteClone(templateNode);
|
node = JQLiteClone(templateNode);
|
||||||
replaceWith(linkRootElement, jqLite(cLinkNode), node);
|
replaceWith(linkRootElement, jqLite(cLinkNode), node);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (replace) {
|
||||||
|
if (cLinkNodeJq.data('$scope')) {
|
||||||
|
// if the original element before replacement had a new scope, the replacement should
|
||||||
|
// get it as well
|
||||||
|
jqLite(node).data('$scope', scope);
|
||||||
|
}
|
||||||
|
dealoc(cLinkNodeJq);
|
||||||
|
}
|
||||||
|
|
||||||
afterWidgetLinkFn(function() {
|
afterWidgetLinkFn(function() {
|
||||||
beforeWidgetLinkFn(afterWidgetChildrenLinkFn, scope, node, rootElement, controller);
|
beforeWidgetLinkFn(afterWidgetChildrenLinkFn, scope, node, rootElement, controller);
|
||||||
}, scope, node, rootElement, controller);
|
}, scope, node, rootElement, controller);
|
||||||
|
|
|
||||||
|
|
@ -991,6 +991,33 @@ describe('$compile', function() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
$compileProvider.directive('tscope' + uppercase(name), function(log) {
|
||||||
|
return {
|
||||||
|
scope: true,
|
||||||
|
restrict: 'CA',
|
||||||
|
templateUrl: 'tscope.html',
|
||||||
|
compile: function() {
|
||||||
|
return function (scope, element) {
|
||||||
|
log(scope.$id);
|
||||||
|
expect(element.data('$scope')).toBe(scope);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
|
$compileProvider.directive('trscope' + uppercase(name), function(log) {
|
||||||
|
return {
|
||||||
|
scope: true,
|
||||||
|
replace: true,
|
||||||
|
restrict: 'CA',
|
||||||
|
templateUrl: 'trscope.html',
|
||||||
|
compile: function() {
|
||||||
|
return function (scope, element) {
|
||||||
|
log(scope.$id);
|
||||||
|
expect(element.data('$scope')).toBe(scope);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
||||||
|
});
|
||||||
$compileProvider.directive('tiscope' + uppercase(name), function(log) {
|
$compileProvider.directive('tiscope' + uppercase(name), function(log) {
|
||||||
return {
|
return {
|
||||||
scope: {},
|
scope: {},
|
||||||
|
|
@ -1034,6 +1061,33 @@ describe('$compile', function() {
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
it('should allow creation of new scopes for directives with templates', inject(
|
||||||
|
function($rootScope, $compile, log, $httpBackend) {
|
||||||
|
$httpBackend.expect('GET', 'tscope.html').respond('<a log>{{name}}; scopeId: {{$id}}</a>');
|
||||||
|
element = $compile('<div><span tscope></span></div>')($rootScope);
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(log).toEqual('LOG; log-002-001; 002');
|
||||||
|
$rootScope.name = 'Jozo';
|
||||||
|
$rootScope.$apply();
|
||||||
|
expect(element.text()).toBe('Jozo; scopeId: 002');
|
||||||
|
expect(element.find('span').scope().$id).toBe('002');
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
it('should allow creation of new scopes for replace directives with templates', inject(
|
||||||
|
function($rootScope, $compile, log, $httpBackend) {
|
||||||
|
$httpBackend.expect('GET', 'trscope.html').
|
||||||
|
respond('<p><a log>{{name}}; scopeId: {{$id}}</a></p>');
|
||||||
|
element = $compile('<div><span trscope></span></div>')($rootScope);
|
||||||
|
$httpBackend.flush();
|
||||||
|
expect(log).toEqual('LOG; log-002-001; 002');
|
||||||
|
$rootScope.name = 'Jozo';
|
||||||
|
$rootScope.$apply();
|
||||||
|
expect(element.text()).toBe('Jozo; scopeId: 002');
|
||||||
|
expect(element.find('a').scope().$id).toBe('002');
|
||||||
|
}));
|
||||||
|
|
||||||
|
|
||||||
it('should allow creation of new isolated scopes for directives with templates', inject(
|
it('should allow creation of new isolated scopes for directives with templates', inject(
|
||||||
function($rootScope, $compile, log, $httpBackend) {
|
function($rootScope, $compile, log, $httpBackend) {
|
||||||
$httpBackend.expect('GET', 'tiscope.html').respond('<a log></a>');
|
$httpBackend.expect('GET', 'tiscope.html').respond('<a log></a>');
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue