From 97c7a4e3791d7cb05c3317cc5f0c49ab93810bf6 Mon Sep 17 00:00:00 2001 From: Misko Hevery Date: Tue, 5 Nov 2013 15:50:53 -0800 Subject: [PATCH] fix($compile): replaced element has isolate scope --- src/ng/compile.js | 37 +++++++++++++-------- test/ng/compileSpec.js | 75 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 14 deletions(-) diff --git a/src/ng/compile.js b/src/ng/compile.js index 7616aa07959a..42b37a2df0f2 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -1237,16 +1237,16 @@ function $CompileProvider($provide) { // combine directives from the original node and from the template: // - take the array of directives for this element - // - split it into two parts, those that were already applied and those that weren't - // - collect directives from the template, add them to the second group and sort them - // - append the second group with new directives to the first group - directives = directives.concat( - collectDirectives( - compileNode, - directives.splice(i + 1, directives.length - (i + 1)), - newTemplateAttrs - ) - ); + // - split it into two parts, those that already applied (processed) and those that weren't (unprocessed) + // - collect directives from the template and sort them by priority + // - combine directives as: processed + template + unprocessed + var templateDirectives = collectDirectives(compileNode, [], newTemplateAttrs); + var unprocessedDirectives = directives.splice(i + 1, directives.length - (i + 1)); + + if (newIsolateScopeDirective) { + markDirectivesAsIsolate(templateDirectives); + } + directives = directives.concat(templateDirectives).concat(unprocessedDirectives); mergeTemplateAttributes(templateAttrs, newTemplateAttrs); ii = directives.length; @@ -1303,13 +1303,13 @@ function $CompileProvider($provide) { if (pre) { if (attrStart) pre = groupElementsLinkFnWrapper(pre, attrStart, attrEnd); pre.require = directive.require; - if (newIsolateScopeDirective === directive) pre.isolateScope = true; + if (newIsolateScopeDirective === directive || directive.$$isolateScope) pre.isolateScope = true; preLinkFns.push(pre); } if (post) { if (attrStart) post = groupElementsLinkFnWrapper(post, attrStart, attrEnd); post.require = directive.require; - if (newIsolateScopeDirective === directive) post.isolateScope = true; + if (newIsolateScopeDirective === directive || directive.$$isolateScope) post.isolateScope = true; postLinkFns.push(post); } } @@ -1501,6 +1501,12 @@ function $CompileProvider($provide) { } } + function markDirectivesAsIsolate(directives) { + // mark all directives as needing isolate scope. + for (var j = 0, jj = directives.length; j < jj; j++) { + directives[j] = inherit(directives[j], {$$isolateScope: true}); + } + } /** * looks up the directive and decorates it with exception handling and proper parameters. We @@ -1616,7 +1622,12 @@ function $CompileProvider($provide) { tempTemplateAttrs = {$attr: {}}; replaceWith($rootElement, $compileNode, compileNode); - collectDirectives(compileNode, directives, tempTemplateAttrs); + var templateDirectives = collectDirectives(compileNode, [], tempTemplateAttrs); + + if (isObject(origAsyncDirective.scope)) { + markDirectivesAsIsolate(templateDirectives); + } + directives = templateDirectives.concat(directives); mergeTemplateAttributes(tAttrs, tempTemplateAttrs); } else { compileNode = beforeTemplateCompileNode; diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 2b6b235f03c6..75c111d72902 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -2450,7 +2450,7 @@ describe('$compile', function() { it('should require controller of an isolate directive from a non-isolate directive on the ' + - 'same element', function() { + 'same element', function() { var IsolateController = function() {}; var isolateDirControllerInNonIsolateDirective; @@ -2480,6 +2480,79 @@ describe('$compile', function() { }); + it('should share isolate scope with replaced directives', function() { + var normalScope; + var isolateScope; + + module(function() { + directive('isolate', function() { + return { + replace: true, + scope: {}, + template: '{{name}}', + link: function(s) { + isolateScope = s; + } + }; + }); + directive('nonIsolate', function() { + return { + link: function(s) { + normalScope = s; + } + }; + }); + }); + + inject(function($compile, $rootScope) { + element = $compile('
')($rootScope); + + expect(normalScope).toBe($rootScope); + expect(normalScope.name).toEqual(undefined); + expect(isolateScope.name).toEqual('WORKS'); + $rootScope.$digest(); + expect(element.text()).toEqual('WORKS'); + }); + }); + + + it('should share isolate scope with replaced directives', function() { + var normalScope; + var isolateScope; + + module(function() { + directive('isolate', function() { + return { + replace: true, + scope: {}, + templateUrl: 'main.html', + link: function(s) { + isolateScope = s; + } + }; + }); + directive('nonIsolate', function() { + return { + link: function(s) { + normalScope = s; + } + }; + }); + }); + + inject(function($compile, $rootScope, $templateCache) { + $templateCache.put('main.html', '{{name}}'); + element = $compile('
')($rootScope); + $rootScope.$apply(); + + expect(normalScope).toBe($rootScope); + expect(normalScope.name).toEqual(undefined); + expect(isolateScope.name).toEqual('WORKS'); + expect(element.text()).toEqual('WORKS'); + }); + }); + + it('should require controller of a non-isolate directive from an isolate directive on the ' + 'same element', function() { var NonIsolateController = function() {};