diff --git a/src/ng/compile.js b/src/ng/compile.js index c49fbe283ec0..3e239dcff291 100644 --- a/src/ng/compile.js +++ b/src/ng/compile.js @@ -585,9 +585,10 @@ function $CompileProvider($provide) { on('$destroy', bind(transcludeScope, transcludeScope.$destroy)); }; })(childTranscludeFn || transcludeFn) - ); + ).performAll(); } else { - nodeLinkFn(childLinkFn, childScope, node, undefined, boundTranscludeFn); + nodeLinkFn(childLinkFn, childScope, node, undefined, boundTranscludeFn) + .performAll(); } } else if (childLinkFn) { childLinkFn(scope, node.childNodes, undefined, boundTranscludeFn); @@ -1067,28 +1068,39 @@ function $CompileProvider($provide) { }); } - // PRELINKING - for(i = 0, ii = preLinkFns.length; i < ii; i++) { - try { - linkFn = preLinkFns[i]; - linkFn(scope, $element, attrs, - linkFn.require && getControllers(linkFn.require, $element)); - } catch (e) { - $exceptionHandler(e, startingTag($element)); - } - } - - // RECURSION - childLinkFn && childLinkFn(scope, linkNode.childNodes, undefined, boundTranscludeFn); - - // POSTLINKING - for(i = 0, ii = postLinkFns.length; i < ii; i++) { - try { - linkFn = postLinkFns[i]; - linkFn(scope, $element, attrs, - linkFn.require && getControllers(linkFn.require, $element)); - } catch (e) { - $exceptionHandler(e, startingTag($element)); + return { + preLinkFn: function() { + // PRELINKING + for(i = 0, ii = preLinkFns.length; i < ii; i++) { + try { + linkFn = preLinkFns[i]; + linkFn(scope, $element, attrs, + linkFn.require && getControllers(linkFn.require, $element)); + } catch (e) { + $exceptionHandler(e, startingTag($element)); + } + } + }, + childLinkFn: function() { + // RECURSION + childLinkFn && childLinkFn(scope, linkNode.childNodes, undefined, boundTranscludeFn); + }, + postLinkFn: function() { + // POSTLINKING + for(i = 0, ii = postLinkFns.length; i < ii; i++) { + try { + linkFn = postLinkFns[i]; + linkFn(scope, $element, attrs, + linkFn.require && getControllers(linkFn.require, $element)); + } catch (e) { + $exceptionHandler(e, startingTag($element)); + } + } + }, + performAll: function() { + this.preLinkFn(); + this.childLinkFn(); + this.postLinkFn(); } } } @@ -1235,10 +1247,16 @@ function $CompileProvider($provide) { replaceWith(linkRootElement, jqLite(beforeTemplateLinkNode), linkNode); } - afterTemplateNodeLinkFn( - beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, linkNode, $rootElement, controller), - scope, linkNode, $rootElement, controller - ); + var beforeNodeLinkFn = beforeTemplateNodeLinkFn(undefined, scope, linkNode, + $rootElement, controller); + var afterNodeLinkFn = afterTemplateNodeLinkFn(undefined, scope, linkNode, + $rootElement, controller); + beforeNodeLinkFn.preLinkFn(); + afterNodeLinkFn && afterNodeLinkFn.preLinkFn(); + afterTemplateChildLinkFn && afterTemplateChildLinkFn(scope, linkNode.childNodes, + undefined, controller); + beforeNodeLinkFn.postLinkFn(); + afterNodeLinkFn && afterNodeLinkFn.postLinkFn(); } linkQueue = null; }). @@ -1252,10 +1270,33 @@ function $CompileProvider($provide) { linkQueue.push(node); linkQueue.push(rootElement); linkQueue.push(controller); + return { + performAll: function() {} + } } else { - afterTemplateNodeLinkFn(function() { - beforeTemplateNodeLinkFn(afterTemplateChildLinkFn, scope, node, rootElement, controller); - }, scope, node, rootElement, controller); + var beforeNodeLinkFn = beforeTemplateNodeLinkFn(undefined, scope, node, rootElement, + controller); + var afterNodeLinkFn = afterTemplateNodeLinkFn(undefined, scope, node, rootElement, + controller); + return { + preLinkFn: function() { + beforeNodeLinkFn.preLinkFn(); + afterNodeLinkFn && afterNodeLinkFn.preLinkFn(); + }, + childLinkFn: function() { + afterTemplateChildLinkFn && afterTemplateChildLinkFn(scope, node.childNodes, + undefined, controller); + }, + postLinkFn: function() { + beforeNodeLinkFn.postLinkFn(); + afterNodeLinkFn && afterNodeLinkFn.postLinkFn(); + }, + performAll: function() { + this.preLinkFn(); + this.childLinkFn(); + this.postLinkFn(); + } + } } }; } diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 2d6485d9bf35..c71ff8eb6e20 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -1046,7 +1046,10 @@ describe('$compile', function() { priority: priority, compile: function() { log(name + '-C'); - return function() { log(name + '-L'); } + return { + pre: function() { log(name + '-P'); }, + post: function() { log(name + '-L'); } + } } }, options || {})); }); @@ -1075,6 +1078,7 @@ describe('$compile', function() { $rootScope.$digest(); expect(log).toEqual( 'first-C; FLUSH; second-C; last-C; third-C; ' + + 'first-P; second-P; last-P; third-P; ' + 'third-L; first-L; second-L; last-L'); var span = element.find('span'); @@ -1099,6 +1103,7 @@ describe('$compile', function() { $rootScope.$digest(); expect(log).toEqual( 'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' + + 'iFirst-P; iSecond-P; iThird-P; iLast-P; ' + 'iFirst-L; iSecond-L; iThird-L; iLast-L'); var div = element.find('div'); @@ -1124,6 +1129,7 @@ describe('$compile', function() { $rootScope.$digest(); expect(log).toEqual( 'first-C; FLUSH; second-C; last-C; third-C; ' + + 'first-P; second-P; last-P; third-P; ' + 'third-L; first-L; second-L; last-L'); var span = element.find('span'); @@ -1149,6 +1155,7 @@ describe('$compile', function() { $rootScope.$digest(); expect(log).toEqual( 'iFirst-C; FLUSH; iSecond-C; iThird-C; iLast-C; ' + + 'iFirst-P; iSecond-P; iThird-P; iLast-P; ' + 'iFirst-L; iSecond-L; iThird-L; iLast-L'); var div = element.find('div'); @@ -2824,11 +2831,8 @@ describe('$compile', function() { }); - it('should make the result of a transclusion available to the parent directive in pre- and post- linking phase (templateUrl)', + it('should make the result of a transclusion available to the parent directive in post- linking phase (templateUrl)', function() { - // when compiling an async directive the transclusion is always processed before the directive - // this is different compared to sync directive. delaying the transclusion makes little sense. - module(function() { directive('trans', function(log) { return { @@ -2850,7 +2854,7 @@ describe('$compile', function() { element = $compile('