Skip to content

Commit 35f3c98

Browse files
committed
fix($compile): fix missing error handling when new-scope directive is applied before isolated-scope directive
Given dirA, which requests new scope, and dirB, which requests new & isolated scope. If dirA is applied *after* dirB, $compile reports an error ('Multiple directives [dirB, dirA] asking for new/isolated scope'), which is expected. However, if dirA is applied *before* dirB, e.g. because dirA has high priority, then no error is thrown. This is the inconsistency. Plus, dirA ends up sharing the isolated scope created for dirB, which may break dirA because it might need to access to parent scope's members. This fix addresses this situation by throwing error regardless of order of directive application. Closes angular#4402
1 parent 08cdd77 commit 35f3c98

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/ng/compile.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -794,18 +794,19 @@ function $CompileProvider($provide) {
794794
}
795795

796796
if (directiveValue = directive.scope) {
797-
newScopeDirective = newScopeDirective || directive;
798-
799797
// skip the check for directives with async templates, we'll check the derived sync directive when
800798
// the template arrives
801799
if (!directive.templateUrl) {
802-
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective, directive, $compileNode);
803800
if (isObject(directiveValue)) {
801+
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective || newScopeDirective, directive, $compileNode);
804802
safeAddClass($compileNode, 'ng-isolate-scope');
805803
newIsolateScopeDirective = directive;
804+
} else {
805+
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective, directive, $compileNode);
806806
}
807807
safeAddClass($compileNode, 'ng-scope');
808808
}
809+
newScopeDirective = newScopeDirective || directive;
809810
}
810811

811812
directiveName = directive.name;

test/ng/compileSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,25 @@ describe('$compile', function() {
15391539
})
15401540
);
15411541

1542+
it('should not allow more than one isolate scope creation per element regardless of directive priority', function() {
1543+
module(function($compileProvider) {
1544+
$compileProvider.directive('highPriorityScope', function() {
1545+
return {
1546+
restrict: 'C',
1547+
priority: 1,
1548+
scope: true,
1549+
link: function() {}
1550+
}
1551+
});
1552+
});
1553+
inject(function($compile) {
1554+
expect(function(){
1555+
$compile('<div class="iscope-a; high-priority-scope"></div>');
1556+
}).toThrowMinErr('$compile', 'multidir', 'Multiple directives [highPriorityScope, iscopeA] asking for new/isolated scope on: ' +
1557+
'<div class="iscope-a; high-priority-scope ng-scope">');
1558+
});
1559+
});
1560+
15421561

15431562
it('should create new scope even at the root of the template', inject(
15441563
function($rootScope, $compile, log) {

0 commit comments

Comments
 (0)