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

Commit 2cde927

Browse files
buunguyenpetebacondarwin
authored andcommitted
fix($compile): always error if two directives add isolate-scope and new-scope
Previously, the compiler would throw an error if a directive requested new non-isolate scope after a directive had requested isolate scope. But it would not error if a directive requested an isolate scope after a directive had requested a new non-isolate scope. Since it is invalid to have more than one directive request any kind of scope if one of them has requested isolate scope, then the compiler should error whatever order the directives are applied. This fix addresses this situation by throwing error regardless of order of directives. BREAKING CHANGE: Requesting isolate scope and any other scope on a single element is an error. Before this change, the compiler let two directives request a child scope and an isolate scope if the compiler applied them in the order of non-isolate scope directive followed by isolate scope directive. Now the compiler will error regardless of the order. If you find that your code is now throwing a `$compile:multidir` error, check that you do not have directives on the same element that are trying to request both an isolate and a non-isolate scope and fix your code. Closes #4402 Closes #4421
1 parent 73e3e85 commit 2cde927

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

src/ng/compile.js

+11-3
Original file line numberDiff line numberDiff line change
@@ -1213,17 +1213,25 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
12131213
}
12141214

12151215
if (directiveValue = directive.scope) {
1216-
newScopeDirective = newScopeDirective || directive;
12171216

12181217
// skip the check for directives with async templates, we'll check the derived sync
12191218
// directive when the template arrives
12201219
if (!directive.templateUrl) {
1221-
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective, directive,
1222-
$compileNode);
12231220
if (isObject(directiveValue)) {
1221+
// This directive is trying to add an isolated scope.
1222+
// Check that there is no scope of any kind already
1223+
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective || newScopeDirective,
1224+
directive, $compileNode);
12241225
newIsolateScopeDirective = directive;
1226+
} else {
1227+
// This directive is trying to add a child scope.
1228+
// Check that there is no isolated scope already
1229+
assertNoDuplicate('new/isolated scope', newIsolateScopeDirective, directive,
1230+
$compileNode);
12251231
}
12261232
}
1233+
1234+
newScopeDirective = newScopeDirective || directive;
12271235
}
12281236

12291237
directiveName = directive.name;

test/ng/compileSpec.js

+19
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,25 @@ describe('$compile', function() {
19891989
})
19901990
);
19911991

1992+
it('should not allow more than one isolate scope creation per element regardless of directive priority', function() {
1993+
module(function($compileProvider) {
1994+
$compileProvider.directive('highPriorityScope', function() {
1995+
return {
1996+
restrict: 'C',
1997+
priority: 1,
1998+
scope: true,
1999+
link: function() {}
2000+
};
2001+
});
2002+
});
2003+
inject(function($compile) {
2004+
expect(function(){
2005+
$compile('<div class="iscope-a; high-priority-scope"></div>');
2006+
}).toThrowMinErr('$compile', 'multidir', 'Multiple directives [highPriorityScope, iscopeA] asking for new/isolated scope on: ' +
2007+
'<div class="iscope-a; high-priority-scope">');
2008+
});
2009+
});
2010+
19922011

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

0 commit comments

Comments
 (0)