@@ -1292,6 +1292,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1292
1292
return function publicLinkFn ( scope , cloneConnectFn , options ) {
1293
1293
assertArg ( scope , 'scope' ) ;
1294
1294
1295
+ if ( previousCompileContext && previousCompileContext . needsNewScope ) {
1296
+ scope = scope . $parent . $new ( ) ;
1297
+ }
1298
+
1295
1299
options = options || { } ;
1296
1300
var parentBoundTranscludeFn = options . parentBoundTranscludeFn ,
1297
1301
transcludeControllers = options . transcludeControllers ,
@@ -1875,7 +1879,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1875
1879
}
1876
1880
1877
1881
$compileNode . empty ( ) ; // clear contents
1878
- childTranscludeFn = compilationGenerator ( mightHaveMultipleTransclusionError , $template , transcludeFn ) ;
1882
+ childTranscludeFn = compilationGenerator ( mightHaveMultipleTransclusionError , $template , transcludeFn , undefined , undefined , { needsNewScope : directive . $$isolateScope || directive . $$newScope } ) ;
1879
1883
childTranscludeFn . $$slots = slots ;
1880
1884
}
1881
1885
}
@@ -1918,8 +1922,11 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
1918
1922
var templateDirectives = collectDirectives ( compileNode , [ ] , newTemplateAttrs ) ;
1919
1923
var unprocessedDirectives = directives . splice ( i + 1 , directives . length - ( i + 1 ) ) ;
1920
1924
1921
- if ( newIsolateScopeDirective ) {
1922
- markDirectivesAsIsolate ( templateDirectives ) ;
1925
+ if ( newIsolateScopeDirective || newScopeDirective ) {
1926
+ // The original directive caused the current element to be replaced but this element
1927
+ // also needs to have a new scope, so we need to tell the template directives
1928
+ // that they would need to get their scope from further up, if they require transclusion
1929
+ markDirectiveScope ( templateDirectives , newIsolateScopeDirective , newScopeDirective ) ;
1923
1930
}
1924
1931
directives = directives . concat ( templateDirectives ) . concat ( unprocessedDirectives ) ;
1925
1932
mergeTemplateAttributes ( templateAttrs , newTemplateAttrs ) ;
@@ -2214,10 +2221,15 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2214
2221
}
2215
2222
}
2216
2223
2217
- function markDirectivesAsIsolate ( directives ) {
2218
- // mark all directives as needing isolate scope.
2224
+ // Depending upon the context in which a directive finds itself it might need to have a new isolated
2225
+ // or child scope created. For instance:
2226
+ // * if the directive has been pulled into a template because another directive with a higher priority
2227
+ // asked for element transclusion
2228
+ // * if the directive itself asks for transclusion but it is at the root of a template and the original
2229
+ // element was replaced. See https://github.com/angular/angular.js/issues/12936
2230
+ function markDirectiveScope ( directives , isolateScope , newScope ) {
2219
2231
for ( var j = 0 , jj = directives . length ; j < jj ; j ++ ) {
2220
- directives [ j ] = inherit ( directives [ j ] , { $$isolateScope : true } ) ;
2232
+ directives [ j ] = inherit ( directives [ j ] , { $$isolateScope : isolateScope , $$newScope : newScope } ) ;
2221
2233
}
2222
2234
}
2223
2235
@@ -2364,7 +2376,9 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
2364
2376
var templateDirectives = collectDirectives ( compileNode , [ ] , tempTemplateAttrs ) ;
2365
2377
2366
2378
if ( isObject ( origAsyncDirective . scope ) ) {
2367
- markDirectivesAsIsolate ( templateDirectives ) ;
2379
+ // the original directive that caused the template to be loaded async required
2380
+ // an isolate scope
2381
+ markDirectiveScope ( templateDirectives , true ) ;
2368
2382
}
2369
2383
directives = templateDirectives . concat ( directives ) ;
2370
2384
mergeTemplateAttributes ( tAttrs , tempTemplateAttrs ) ;
0 commit comments