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

Isolated scope properties are not available in 'pre' link function when using 'templateUrl' #4193

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 72 additions & 31 deletions src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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();
}
}
}
Expand Down Expand Up @@ -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;
}).
Expand All @@ -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();
}
}
}
};
}
Expand Down
16 changes: 10 additions & 6 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {}));
});
Expand Down Expand Up @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand All @@ -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');
Expand Down Expand Up @@ -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 {
Expand All @@ -2850,7 +2854,7 @@ describe('$compile', function() {

element = $compile('<div trans><span>unicorn!</span></div>')($rootScope);
$rootScope.$apply();
expect(log).toEqual('pre(unicorn!); post(unicorn!)');
expect(log).toEqual('pre(); post(unicorn!)');
});
});
});
Expand Down