@@ -3438,6 +3438,113 @@ describe('$compile', function() {
3438
3438
expect ( log ) . toEqual ( 'pre(); post(unicorn!)' ) ;
3439
3439
} ) ;
3440
3440
} ) ;
3441
+
3442
+ it ( 'should copy the directive controller to all clones' , function ( ) {
3443
+ var transcludeCtrl , cloneCount = 2 ;
3444
+ module ( function ( ) {
3445
+ directive ( 'transclude' , valueFn ( {
3446
+ transclude : 'content' ,
3447
+ controller : function ( $transclude ) {
3448
+ transcludeCtrl = this ;
3449
+ } ,
3450
+ link : function ( scope , el , attr , ctrl , $transclude ) {
3451
+ var i ;
3452
+ for ( i = 0 ; i < cloneCount ; i ++ ) {
3453
+ $transclude ( cloneAttach ) ;
3454
+ }
3455
+
3456
+ function cloneAttach ( clone ) {
3457
+ el . append ( clone ) ;
3458
+ }
3459
+ }
3460
+ } ) ) ;
3461
+ } ) ;
3462
+ inject ( function ( $compile ) {
3463
+ element = $compile ( '<div transclude><span></span></div>' ) ( $rootScope ) ;
3464
+ var children = element . children ( ) , i ;
3465
+ expect ( transcludeCtrl ) . toBeDefined ( ) ;
3466
+
3467
+ expect ( element . data ( '$transcludeController' ) ) . toBe ( transcludeCtrl ) ;
3468
+ for ( i = 0 ; i < cloneCount ; i ++ ) {
3469
+ expect ( children . eq ( i ) . data ( '$transcludeController' ) ) . toBeUndefined ( ) ;
3470
+ }
3471
+ } ) ;
3472
+ } ) ;
3473
+
3474
+ it ( 'should provide the $transclude controller local as 5th argument to the pre and post-link function' , function ( ) {
3475
+ var ctrlTransclude , preLinkTransclude , postLinkTransclude ;
3476
+ module ( function ( ) {
3477
+ directive ( 'transclude' , valueFn ( {
3478
+ transclude : 'content' ,
3479
+ controller : function ( $transclude ) {
3480
+ ctrlTransclude = $transclude ;
3481
+ } ,
3482
+ compile : function ( ) {
3483
+ return {
3484
+ pre : function ( scope , el , attr , ctrl , $transclude ) {
3485
+ preLinkTransclude = $transclude ;
3486
+ } ,
3487
+ post : function ( scope , el , attr , ctrl , $transclude ) {
3488
+ postLinkTransclude = $transclude ;
3489
+ }
3490
+ } ;
3491
+ }
3492
+ } ) ) ;
3493
+ } ) ;
3494
+ inject ( function ( $compile ) {
3495
+ element = $compile ( '<div transclude></div>' ) ( $rootScope ) ;
3496
+ expect ( ctrlTransclude ) . toBeDefined ( ) ;
3497
+ expect ( ctrlTransclude ) . toBe ( preLinkTransclude ) ;
3498
+ expect ( ctrlTransclude ) . toBe ( postLinkTransclude ) ;
3499
+ } ) ;
3500
+ } ) ;
3501
+
3502
+ it ( 'should allow an optional scope argument in $transclude' , function ( ) {
3503
+ var capturedChildCtrl ;
3504
+ module ( function ( ) {
3505
+ directive ( 'transclude' , valueFn ( {
3506
+ transclude : 'content' ,
3507
+ link : function ( scope , element , attr , ctrl , $transclude ) {
3508
+ $transclude ( scope , function ( clone ) {
3509
+ element . append ( clone ) ;
3510
+ } ) ;
3511
+ }
3512
+ } ) ) ;
3513
+ } ) ;
3514
+ inject ( function ( $compile ) {
3515
+ element = $compile ( '<div transclude>{{$id}}</div>' ) ( $rootScope ) ;
3516
+ $rootScope . $apply ( ) ;
3517
+ expect ( element . text ( ) ) . toBe ( $rootScope . $id ) ;
3518
+ } ) ;
3519
+
3520
+ } ) ;
3521
+
3522
+ it ( 'should expose the directive controller to transcluded children' , function ( ) {
3523
+ var capturedChildCtrl ;
3524
+ module ( function ( ) {
3525
+ directive ( 'transclude' , valueFn ( {
3526
+ transclude : 'content' ,
3527
+ controller : function ( ) {
3528
+ } ,
3529
+ link : function ( scope , element , attr , ctrl , $transclude ) {
3530
+ $transclude ( function ( clone ) {
3531
+ element . append ( clone ) ;
3532
+ } ) ;
3533
+ }
3534
+ } ) ) ;
3535
+ directive ( 'child' , valueFn ( {
3536
+ require : '^transclude' ,
3537
+ link : function ( scope , element , attr , ctrl ) {
3538
+ capturedChildCtrl = ctrl ;
3539
+ }
3540
+ } ) ) ;
3541
+ } ) ;
3542
+ inject ( function ( $compile ) {
3543
+ element = $compile ( '<div transclude><div child></div></div>' ) ( $rootScope ) ;
3544
+ expect ( capturedChildCtrl ) . toBeTruthy ( ) ;
3545
+ } ) ;
3546
+
3547
+ } ) ;
3441
3548
} ) ;
3442
3549
3443
3550
@@ -3471,7 +3578,6 @@ describe('$compile', function() {
3471
3578
} ) ;
3472
3579
} ) ;
3473
3580
3474
-
3475
3581
it ( 'should only allow one element transclusion per element' , function ( ) {
3476
3582
module ( function ( ) {
3477
3583
directive ( 'first' , valueFn ( {
@@ -3620,8 +3726,101 @@ describe('$compile', function() {
3620
3726
] ) ;
3621
3727
} ) ;
3622
3728
} ) ;
3623
- } ) ;
3624
3729
3730
+ it ( 'should allow to access $transclude in the same directive' , function ( ) {
3731
+ var _$transclude ;
3732
+ module ( function ( ) {
3733
+ directive ( 'transclude' , valueFn ( {
3734
+ transclude : 'element' ,
3735
+ controller : function ( $transclude ) {
3736
+ _$transclude = $transclude ;
3737
+ }
3738
+ } ) ) ;
3739
+ } ) ;
3740
+ inject ( function ( $compile ) {
3741
+ element = $compile ( '<div transclude></div>' ) ( $rootScope ) ;
3742
+ expect ( _$transclude ) . toBeDefined ( )
3743
+ } ) ;
3744
+ } ) ;
3745
+
3746
+ it ( 'should copy the directive controller to all clones' , function ( ) {
3747
+ var transcludeCtrl , cloneCount = 2 ;
3748
+ module ( function ( ) {
3749
+ directive ( 'transclude' , valueFn ( {
3750
+ transclude : 'element' ,
3751
+ controller : function ( ) {
3752
+ transcludeCtrl = this ;
3753
+ } ,
3754
+ link : function ( scope , el , attr , ctrl , $transclude ) {
3755
+ var i ;
3756
+ for ( i = 0 ; i < cloneCount ; i ++ ) {
3757
+ $transclude ( cloneAttach ) ;
3758
+ }
3759
+
3760
+ function cloneAttach ( clone ) {
3761
+ el . after ( clone ) ;
3762
+ }
3763
+ }
3764
+ } ) ) ;
3765
+ } ) ;
3766
+ inject ( function ( $compile ) {
3767
+ element = $compile ( '<div><div transclude></div></div>' ) ( $rootScope ) ;
3768
+ var children = element . children ( ) , i ;
3769
+ for ( i = 0 ; i < cloneCount ; i ++ ) {
3770
+ expect ( children . eq ( i ) . data ( '$transcludeController' ) ) . toBe ( transcludeCtrl ) ;
3771
+ }
3772
+ } ) ;
3773
+ } ) ;
3774
+
3775
+ it ( 'should expose the directive controller to transcluded children' , function ( ) {
3776
+ var capturedTranscludeCtrl ;
3777
+ module ( function ( ) {
3778
+ directive ( 'transclude' , valueFn ( {
3779
+ transclude : 'element' ,
3780
+ controller : function ( ) {
3781
+ } ,
3782
+ link : function ( scope , element , attr , ctrl , $transclude ) {
3783
+ $transclude ( scope , function ( clone ) {
3784
+ element . after ( clone ) ;
3785
+ } ) ;
3786
+ }
3787
+ } ) ) ;
3788
+ directive ( 'child' , valueFn ( {
3789
+ require : '^transclude' ,
3790
+ link : function ( scope , element , attr , ctrl ) {
3791
+ capturedTranscludeCtrl = ctrl ;
3792
+ }
3793
+ } ) ) ;
3794
+ } ) ;
3795
+ inject ( function ( $compile ) {
3796
+ element = $compile ( '<div transclude><div child></div></div>' ) ( $rootScope ) ;
3797
+ expect ( capturedTranscludeCtrl ) . toBeTruthy ( ) ;
3798
+ } ) ;
3799
+ } ) ;
3800
+
3801
+ it ( 'should allow access to $transclude in a templateUrl directive' , function ( ) {
3802
+ var transclude ;
3803
+ module ( function ( ) {
3804
+ directive ( 'template' , valueFn ( {
3805
+ templateUrl : 'template.html' ,
3806
+ replace : true
3807
+ } ) ) ;
3808
+ directive ( 'transclude' , valueFn ( {
3809
+ transclude : 'content' ,
3810
+ controller : function ( $transclude ) {
3811
+ transclude = $transclude ;
3812
+ }
3813
+ } ) ) ;
3814
+ } ) ;
3815
+ inject ( function ( $compile , $httpBackend ) {
3816
+ $httpBackend . expectGET ( 'template.html' ) . respond ( '<div transclude></div>' ) ;
3817
+ element = $compile ( '<div template></div>' ) ( $rootScope ) ;
3818
+ $httpBackend . flush ( ) ;
3819
+ expect ( transclude ) . toBeDefined ( ) ;
3820
+ } ) ;
3821
+ } ) ;
3822
+
3823
+ } ) ;
3625
3824
3626
3825
it ( 'should safely create transclude comment node and not break with "-->"' ,
3627
3826
inject ( function ( $rootScope ) {
0 commit comments