aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIgor Minar2013-08-20 16:08:24 -0700
committerIgor Minar2013-08-20 23:31:38 -0700
commitbf79bd4194eca2118ae1c492c08dbd217f5ae810 (patch)
treedeca3f1e700eba811a226917fd18a6d994b33215
parent5c4ffb36deee18743beb149ceae4d63d5b1ae8d9 (diff)
downloadangular.js-bf79bd4194eca2118ae1c492c08dbd217f5ae810.tar.bz2
fix(ngTransclude): make the transclusion available to parent post-link
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.
-rw-r--r--src/ng/directive/ngTransclude.js20
-rwxr-xr-xtest/ng/compileSpec.js59
2 files changed, 69 insertions, 10 deletions
diff --git a/src/ng/directive/ngTransclude.js b/src/ng/directive/ngTransclude.js
index 668f8033..0b9b919d 100644
--- a/src/ng/directive/ngTransclude.js
+++ b/src/ng/directive/ngTransclude.js
@@ -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);
});
- }]
+ }
});
diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js
index 28dc57fe..7c24e1d9 100755
--- a/test/ng/compileSpec.js
+++ b/test/ng/compileSpec.js
@@ -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';