From 90f87072e83234ae366cfeb3c281503c31dad738 Mon Sep 17 00:00:00 2001 From: Tobias Bosch Date: Thu, 14 Nov 2013 13:50:36 -0800 Subject: fix($compile): accessing controllers of transcluded directives from children Additional API (backwards compatible) - Injects `$transclude` (see directive controllers) as 5th argument to directive link functions. - `$transclude` takes an optional scope as first parameter that overrides the bound scope. Deprecations: - `transclude` parameter of directive compile functions (use the new parameter for link functions instead). Refactorings: - Don't use comment node to temporarily store controllers - `ngIf`, `ngRepeat`, ... now all use `$transclude` Closes #4935. --- test/ng/compileSpec.js | 203 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 201 insertions(+), 2 deletions(-) (limited to 'test/ng/compileSpec.js') diff --git a/test/ng/compileSpec.js b/test/ng/compileSpec.js index 3e35fae7..85ee17d2 100755 --- a/test/ng/compileSpec.js +++ b/test/ng/compileSpec.js @@ -3438,6 +3438,113 @@ describe('$compile', function() { expect(log).toEqual('pre(); post(unicorn!)'); }); }); + + it('should copy the directive controller to all clones', function() { + var transcludeCtrl, cloneCount = 2; + module(function() { + directive('transclude', valueFn({ + transclude: 'content', + controller: function($transclude) { + transcludeCtrl = this; + }, + link: function(scope, el, attr, ctrl, $transclude) { + var i; + for (i=0; i')($rootScope); + var children = element.children(), i; + expect(transcludeCtrl).toBeDefined(); + + expect(element.data('$transcludeController')).toBe(transcludeCtrl); + for (i=0; i')($rootScope); + expect(ctrlTransclude).toBeDefined(); + expect(ctrlTransclude).toBe(preLinkTransclude); + expect(ctrlTransclude).toBe(postLinkTransclude); + }); + }); + + it('should allow an optional scope argument in $transclude', function() { + var capturedChildCtrl; + module(function() { + directive('transclude', valueFn({ + transclude: 'content', + link: function(scope, element, attr, ctrl, $transclude) { + $transclude(scope, function(clone) { + element.append(clone); + }); + } + })); + }); + inject(function($compile) { + element = $compile('
{{$id}}
')($rootScope); + $rootScope.$apply(); + expect(element.text()).toBe($rootScope.$id); + }); + + }); + + it('should expose the directive controller to transcluded children', function() { + var capturedChildCtrl; + module(function() { + directive('transclude', valueFn({ + transclude: 'content', + controller: function() { + }, + link: function(scope, element, attr, ctrl, $transclude) { + $transclude(function(clone) { + element.append(clone); + }); + } + })); + directive('child', valueFn({ + require: '^transclude', + link: function(scope, element, attr, ctrl) { + capturedChildCtrl = ctrl; + } + })); + }); + inject(function($compile) { + element = $compile('
')($rootScope); + expect(capturedChildCtrl).toBeTruthy(); + }); + + }); }); @@ -3471,7 +3578,6 @@ describe('$compile', function() { }); }); - it('should only allow one element transclusion per element', function() { module(function() { directive('first', valueFn({ @@ -3620,8 +3726,101 @@ describe('$compile', function() { ]); }); }); - }); + it('should allow to access $transclude in the same directive', function() { + var _$transclude; + module(function() { + directive('transclude', valueFn({ + transclude: 'element', + controller: function($transclude) { + _$transclude = $transclude; + } + })); + }); + inject(function($compile) { + element = $compile('
')($rootScope); + expect(_$transclude).toBeDefined() + }); + }); + + it('should copy the directive controller to all clones', function() { + var transcludeCtrl, cloneCount = 2; + module(function() { + directive('transclude', valueFn({ + transclude: 'element', + controller: function() { + transcludeCtrl = this; + }, + link: function(scope, el, attr, ctrl, $transclude) { + var i; + for (i=0; i
')($rootScope); + var children = element.children(), i; + for (i=0; i
')($rootScope); + expect(capturedTranscludeCtrl).toBeTruthy(); + }); + }); + + it('should allow access to $transclude in a templateUrl directive', function() { + var transclude; + module(function() { + directive('template', valueFn({ + templateUrl: 'template.html', + replace: true + })); + directive('transclude', valueFn({ + transclude: 'content', + controller: function($transclude) { + transclude = $transclude; + } + })); + }); + inject(function($compile, $httpBackend) { + $httpBackend.expectGET('template.html').respond('
'); + element = $compile('
')($rootScope); + $httpBackend.flush(); + expect(transclude).toBeDefined(); + }); + }); + + }); it('should safely create transclude comment node and not break with "-->"', inject(function($rootScope) { -- cgit v1.2.3