@@ -5354,6 +5354,195 @@ describe('$compile', function() {
5354
5354
} ) ;
5355
5355
} ) ;
5356
5356
5357
+ it ( 'should bind the required controllers to the directive controller, if provided as an object and bindToController is truthy' , function ( ) {
5358
+ var parentController , siblingController ;
5359
+
5360
+ function ParentController ( ) { this . name = 'Parent' ; }
5361
+ function SiblingController ( ) { this . name = 'Sibling' ; }
5362
+ function MeController ( ) { this . name = 'Me' ; }
5363
+ MeController . prototype . $onInit = function ( ) {
5364
+ parentController = this . container ;
5365
+ siblingController = this . friend ;
5366
+ } ;
5367
+ spyOn ( MeController . prototype , '$onInit' ) . andCallThrough ( ) ;
5368
+
5369
+ angular . module ( 'my' , [ ] )
5370
+ . directive ( 'me' , function ( ) {
5371
+ return {
5372
+ restrict : 'E' ,
5373
+ scope : { } ,
5374
+ require : { container : '^parent' , friend : 'sibling' } ,
5375
+ bindToController : true ,
5376
+ controller : MeController ,
5377
+ controllerAs : '$ctrl'
5378
+ } ;
5379
+ } )
5380
+ . directive ( 'parent' , function ( ) {
5381
+ return {
5382
+ restrict : 'E' ,
5383
+ scope : { } ,
5384
+ controller : ParentController
5385
+ } ;
5386
+ } )
5387
+ . directive ( 'sibling' , function ( ) {
5388
+ return {
5389
+ controller : SiblingController
5390
+ } ;
5391
+ } ) ;
5392
+
5393
+ module ( 'my' ) ;
5394
+ inject ( function ( $compile , $rootScope , meDirective ) {
5395
+ element = $compile ( '<parent><me sibling></me></parent>' ) ( $rootScope ) ;
5396
+ expect ( MeController . prototype . $onInit ) . toHaveBeenCalled ( ) ;
5397
+ expect ( parentController ) . toEqual ( jasmine . any ( ParentController ) ) ;
5398
+ expect ( siblingController ) . toEqual ( jasmine . any ( SiblingController ) ) ;
5399
+ } ) ;
5400
+ } ) ;
5401
+
5402
+
5403
+ it ( 'should not bind required controllers if bindToController is falsy' , function ( ) {
5404
+ var parentController , siblingController ;
5405
+
5406
+ function ParentController ( ) { this . name = 'Parent' ; }
5407
+ function SiblingController ( ) { this . name = 'Sibling' ; }
5408
+ function MeController ( ) { this . name = 'Me' ; }
5409
+ MeController . prototype . $onInit = function ( ) {
5410
+ parentController = this . container ;
5411
+ siblingController = this . friend ;
5412
+ } ;
5413
+ spyOn ( MeController . prototype , '$onInit' ) . andCallThrough ( ) ;
5414
+
5415
+ angular . module ( 'my' , [ ] )
5416
+ . directive ( 'me' , function ( ) {
5417
+ return {
5418
+ restrict : 'E' ,
5419
+ scope : { } ,
5420
+ require : { container : '^parent' , friend : 'sibling' } ,
5421
+ controller : MeController
5422
+ } ;
5423
+ } )
5424
+ . directive ( 'parent' , function ( ) {
5425
+ return {
5426
+ restrict : 'E' ,
5427
+ scope : { } ,
5428
+ controller : ParentController
5429
+ } ;
5430
+ } )
5431
+ . directive ( 'sibling' , function ( ) {
5432
+ return {
5433
+ controller : SiblingController
5434
+ } ;
5435
+ } ) ;
5436
+
5437
+ module ( 'my' ) ;
5438
+ inject ( function ( $compile , $rootScope , meDirective ) {
5439
+ element = $compile ( '<parent><me sibling></me></parent>' ) ( $rootScope ) ;
5440
+ expect ( MeController . prototype . $onInit ) . toHaveBeenCalled ( ) ;
5441
+ expect ( parentController ) . toBeUndefined ( ) ;
5442
+ expect ( siblingController ) . toBeUndefined ( ) ;
5443
+ } ) ;
5444
+ } ) ;
5445
+
5446
+ it ( 'should bind required controllers to controller that has an explicit constructor return value' , function ( ) {
5447
+ var parentController , siblingController , meController ;
5448
+
5449
+ function ParentController ( ) { this . name = 'Parent' ; }
5450
+ function SiblingController ( ) { this . name = 'Sibling' ; }
5451
+ function MeController ( ) {
5452
+ meController = {
5453
+ name : 'Me' ,
5454
+ $onInit : function ( ) {
5455
+ parentController = this . container ;
5456
+ siblingController = this . friend ;
5457
+ }
5458
+ } ;
5459
+ spyOn ( meController , '$onInit' ) . andCallThrough ( ) ;
5460
+ return meController ;
5461
+ }
5462
+
5463
+ angular . module ( 'my' , [ ] )
5464
+ . directive ( 'me' , function ( ) {
5465
+ return {
5466
+ restrict : 'E' ,
5467
+ scope : { } ,
5468
+ require : { container : '^parent' , friend : 'sibling' } ,
5469
+ bindToController : true ,
5470
+ controller : MeController ,
5471
+ controllerAs : '$ctrl'
5472
+ } ;
5473
+ } )
5474
+ . directive ( 'parent' , function ( ) {
5475
+ return {
5476
+ restrict : 'E' ,
5477
+ scope : { } ,
5478
+ controller : ParentController
5479
+ } ;
5480
+ } )
5481
+ . directive ( 'sibling' , function ( ) {
5482
+ return {
5483
+ controller : SiblingController
5484
+ } ;
5485
+ } ) ;
5486
+
5487
+ module ( 'my' ) ;
5488
+ inject ( function ( $compile , $rootScope , meDirective ) {
5489
+ element = $compile ( '<parent><me sibling></me></parent>' ) ( $rootScope ) ;
5490
+ expect ( meController . $onInit ) . toHaveBeenCalled ( ) ;
5491
+ expect ( parentController ) . toEqual ( jasmine . any ( ParentController ) ) ;
5492
+ expect ( siblingController ) . toEqual ( jasmine . any ( SiblingController ) ) ;
5493
+ } ) ;
5494
+ } ) ;
5495
+
5496
+
5497
+ it ( 'should bind required controllers to controllers that return an explicit constructor return value' , function ( ) {
5498
+ var parentController , containerController , siblingController , friendController , meController ;
5499
+
5500
+ function MeController ( ) {
5501
+ this . name = 'Me' ;
5502
+ this . $onInit = function ( ) {
5503
+ containerController = this . container ;
5504
+ friendController = this . friend ;
5505
+ } ;
5506
+ }
5507
+ function ParentController ( ) {
5508
+ return parentController = { name : 'Parent' } ;
5509
+ }
5510
+ function SiblingController ( ) {
5511
+ return siblingController = { name : 'Sibling' } ;
5512
+ }
5513
+
5514
+ angular . module ( 'my' , [ ] )
5515
+ . directive ( 'me' , function ( ) {
5516
+ return {
5517
+ priority : 1 , // make sure it is run before sibling to test this case correctly
5518
+ restrict : 'E' ,
5519
+ scope : { } ,
5520
+ require : { container : '^parent' , friend : 'sibling' } ,
5521
+ bindToController : true ,
5522
+ controller : MeController ,
5523
+ controllerAs : '$ctrl'
5524
+ } ;
5525
+ } )
5526
+ . directive ( 'parent' , function ( ) {
5527
+ return {
5528
+ restrict : 'E' ,
5529
+ scope : { } ,
5530
+ controller : ParentController
5531
+ } ;
5532
+ } )
5533
+ . directive ( 'sibling' , function ( ) {
5534
+ return {
5535
+ controller : SiblingController
5536
+ } ;
5537
+ } ) ;
5538
+
5539
+ module ( 'my' ) ;
5540
+ inject ( function ( $compile , $rootScope , meDirective ) {
5541
+ element = $compile ( '<parent><me sibling></me></parent>' ) ( $rootScope ) ;
5542
+ expect ( containerController ) . toEqual ( parentController ) ;
5543
+ expect ( friendController ) . toEqual ( siblingController ) ;
5544
+ } ) ;
5545
+ } ) ;
5357
5546
5358
5547
it ( 'should require controller of an isolate directive from a non-isolate directive on the ' +
5359
5548
'same element' , function ( ) {
@@ -5728,7 +5917,7 @@ describe('$compile', function() {
5728
5917
return {
5729
5918
require : { myC1 : '^c1' , myC2 : '^c2' } ,
5730
5919
link : function ( scope , element , attrs , controllers ) {
5731
- log ( 'dep:' + controllers . myC1 . name + '-' + controller . myC2 . name ) ;
5920
+ log ( 'dep:' + controllers . myC1 . name + '-' + controllers . myC2 . name ) ;
5732
5921
}
5733
5922
} ;
5734
5923
} ) ;
0 commit comments