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

fix($compile): set $isolateScope correctly for sync template directives #6942

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,8 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {

isolateScope = scope.$new(true);

if (templateDirective && (templateDirective === newIsolateScopeDirective.$$originalDirective)) {
if (templateDirective && (templateDirective === newIsolateScopeDirective ||
templateDirective === newIsolateScopeDirective.$$originalDirective)) {
$linkNode.data('$isolateScope', isolateScope) ;
} else {
$linkNode.data('$isolateScopeNoTemplate', isolateScope);
Expand Down
53 changes: 53 additions & 0 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1797,6 +1797,19 @@ describe('$compile', function() {
}
};
});
directive('stscope' + uppercase(name), function(log) {
return {
scope: true,
restrict: 'CA',
template: '<span></span>',
compile: function() {
return function (scope, element) {
log(scope.$id);
expect(element.data('$scope')).toBe(scope);
};
}
};
});
directive('trscope' + uppercase(name), function(log) {
return {
scope: true,
Expand Down Expand Up @@ -1825,6 +1838,20 @@ describe('$compile', function() {
}
};
});
directive('stiscope' + uppercase(name), function(log) {
return {
scope: {},
restrict: 'CA',
template: '<span></span>',
compile: function() {
return function (scope, element) {
iscope = scope;
log(scope.$id);
expect(element.data('$isolateScope')).toBe(scope);
};
}
};
});
});
directive('log', function(log) {
return {
Expand Down Expand Up @@ -1999,6 +2026,13 @@ describe('$compile', function() {
expect(element.find('a').scope().$parent).toBe($rootScope);
})
);

it('should return the new scope for children in the directive sync template', inject(
function($rootScope, $compile) {
element = $compile('<div stscope></div>')($rootScope);
expect(element.find('span').scope().$parent).toBe($rootScope);
})
);
});


Expand Down Expand Up @@ -2041,6 +2075,14 @@ describe('$compile', function() {
expect(element.isolateScope()).not.toBe($rootScope);
})
);

it('should return the isolate scope for children in directive sync template', inject(
function($rootScope, $compile) {
element = $compile('<div stiscope></div>')($rootScope);
expect(element.find('span').scope()).toBe(element.isolateScope());
expect(element.isolateScope()).not.toBe($rootScope);
})
);
});


Expand Down Expand Up @@ -2070,6 +2112,17 @@ describe('$compile', function() {
expect(child.scope()).toBe(directiveElement.isolateScope());
})
);

it('should return the isolate scope for child elements in directive sync template', inject(
function($rootScope, $compile) {
var directiveElement, child;
element = $compile('<div><a ng-if="true" stiscope></a></div>')($rootScope);
$rootScope.$apply();
directiveElement = element.find('a');
child = directiveElement.find('span');
expect(child.scope()).toBe(directiveElement.isolateScope());
})
);
});
});
});
Expand Down