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