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

Commit

Permalink
fix(ngTransclude): make the transclusion available to parent post-link
Browse files Browse the repository at this point in the history
previously the translusion was appended the the ngTranslude element via
$evalAsync which makes the transluded dom unavailable to parent
post-linking functions. By appending translusion in linking phase,
post-linking functions will be able to access it.
  • Loading branch information
IgorMinar committed Aug 21, 2013
1 parent 5c4ffb3 commit bf79bd4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 10 deletions.
20 changes: 11 additions & 9 deletions src/ng/directive/ngTransclude.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@
*
*/
var ngTranscludeDirective = ngDirective({
controller: ['$transclude', '$element', '$scope', function($transclude, $element, $scope) {
// use evalAsync so that we don't process transclusion before directives on the parent element even when the
// transclusion replaces the current element. (we can't use priority here because that applies only to compile fns
// and not controllers
$scope.$evalAsync(function() {
$transclude(function(clone) {
$element.append(clone);
});
controller: ['$transclude', function($transclude) {
// remember the transclusion fn but call it during linking so that we don't process transclusion before directives on
// the parent element even when the transclusion replaces the current element. (we can't use priority here because
// that applies only to compile fns and not controllers
this.$transclude = $transclude;
}],

link: function($scope, $element, $attrs, controller) {
controller.$transclude(function(clone) {
$element.append(clone);
});
}]
}
});
59 changes: 58 additions & 1 deletion test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2823,10 +2823,67 @@ describe('$compile', function() {
expect(jqLite(element.find('span')[1]).text()).toEqual('T:true');
});
});


it('should make the result of a transclusion available to the parent directive in post-linking phase (template)',
function() {
module(function() {
directive('trans', function(log) {
return {
transclude: true,
template: '<div ng-transclude></div>',
link: {
pre: function($scope, $element) {
log('pre(' + $element.text() + ')');
},
post: function($scope, $element) {
log('post(' + $element.text() + ')');
}
}
};
});
});
inject(function(log, $rootScope, $compile) {
element = $compile('<div trans><span>unicorn!</span></div>')($rootScope);
$rootScope.$apply();
expect(log).toEqual('pre(); post(unicorn!)');
});
});


it('should make the result of a transclusion available to the parent directive in pre- and 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 {
transclude: true,
templateUrl: 'trans.html',
link: {
pre: function($scope, $element) {
log('pre(' + $element.text() + ')');
},
post: function($scope, $element) {
log('post(' + $element.text() + ')');
}
}
};
});
});
inject(function(log, $rootScope, $compile, $templateCache) {
$templateCache.put('trans.html', '<div ng-transclude></div>');

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


describe('img[src] sanitization', function($sce) {
describe('img[src] sanitization', function() {
it('should NOT require trusted values for img src', inject(function($rootScope, $compile, $sce) {
element = $compile('<img src="{{testUrl}}"></img>')($rootScope);
$rootScope.testUrl = 'http://example.com/image.png';
Expand Down

2 comments on commit bf79bd4

@bradw2k
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this fix #3558 ?

@bradw2k
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does not seem to fix 3558. The transcluded content is not available during link() function in 1.2.0rc1 nor 1.2.0-rc.2, but it WAS in 1.1.5:
http://plnkr.co/edit/JiphJK?p=preview

Please sign in to comment.