@@ -6268,6 +6268,89 @@ describe('$compile', function() { |
6268 | 6268 | }); |
6269 | 6269 | }); |
6270 | 6270 |
|
| 6271 | + it('should use the key if the name of a required controller is omitted', function() { |
| 6272 | + function ParentController() { this.name = 'Parent'; } |
| 6273 | + function ParentOptController() { this.name = 'ParentOpt'; } |
| 6274 | + function ParentOrSiblingController() { this.name = 'ParentOrSibling'; } |
| 6275 | + function ParentOrSiblingOptController() { this.name = 'ParentOrSiblingOpt'; } |
| 6276 | + function SiblingController() { this.name = 'Sibling'; } |
| 6277 | + function SiblingOptController() { this.name = 'SiblingOpt'; } |
| 6278 | + |
| 6279 | + angular.module('my', []) |
| 6280 | + .component('me', { |
| 6281 | + require: { |
| 6282 | + parent: '^^', |
| 6283 | + parentOpt: '?^^', |
| 6284 | + parentOrSibling1: '^', |
| 6285 | + parentOrSiblingOpt1: '?^', |
| 6286 | + parentOrSibling2: '^', |
| 6287 | + parentOrSiblingOpt2: '?^', |
| 6288 | + sibling: '', |
| 6289 | + siblingOpt: '?' |
| 6290 | + } |
| 6291 | + }) |
| 6292 | + .directive('parent', function() { |
| 6293 | + return {controller: ParentController}; |
| 6294 | + }) |
| 6295 | + .directive('parentOpt', function() { |
| 6296 | + return {controller: ParentOptController}; |
| 6297 | + }) |
| 6298 | + .directive('parentOrSibling1', function() { |
| 6299 | + return {controller: ParentOrSiblingController}; |
| 6300 | + }) |
| 6301 | + .directive('parentOrSiblingOpt1', function() { |
| 6302 | + return {controller: ParentOrSiblingOptController}; |
| 6303 | + }) |
| 6304 | + .directive('parentOrSibling2', function() { |
| 6305 | + return {controller: ParentOrSiblingController}; |
| 6306 | + }) |
| 6307 | + .directive('parentOrSiblingOpt2', function() { |
| 6308 | + return {controller: ParentOrSiblingOptController}; |
| 6309 | + }) |
| 6310 | + .directive('sibling', function() { |
| 6311 | + return {controller: SiblingController}; |
| 6312 | + }) |
| 6313 | + .directive('siblingOpt', function() { |
| 6314 | + return {controller: SiblingOptController}; |
| 6315 | + }); |
| 6316 | + |
| 6317 | + module('my'); |
| 6318 | + inject(function($compile, $rootScope) { |
| 6319 | + var template = |
| 6320 | + '<div>' + |
| 6321 | + // With optional |
| 6322 | + '<parent parent-opt parent-or-sibling-1 parent-or-sibling-opt-1>' + |
| 6323 | + '<me parent-or-sibling-2 parent-or-sibling-opt-2 sibling sibling-opt></me>' + |
| 6324 | + '</parent>' + |
| 6325 | + // Without optional |
| 6326 | + '<parent parent-or-sibling-1>' + |
| 6327 | + '<me parent-or-sibling-2 sibling></me>' + |
| 6328 | + '</parent>' + |
| 6329 | + '</div>'; |
| 6330 | + element = $compile(template)($rootScope); |
| 6331 | + |
| 6332 | + var ctrl1 = element.find('me').eq(0).controller('me'); |
| 6333 | + expect(ctrl1.parent).toEqual(jasmine.any(ParentController)); |
| 6334 | + expect(ctrl1.parentOpt).toEqual(jasmine.any(ParentOptController)); |
| 6335 | + expect(ctrl1.parentOrSibling1).toEqual(jasmine.any(ParentOrSiblingController)); |
| 6336 | + expect(ctrl1.parentOrSiblingOpt1).toEqual(jasmine.any(ParentOrSiblingOptController)); |
| 6337 | + expect(ctrl1.parentOrSibling2).toEqual(jasmine.any(ParentOrSiblingController)); |
| 6338 | + expect(ctrl1.parentOrSiblingOpt2).toEqual(jasmine.any(ParentOrSiblingOptController)); |
| 6339 | + expect(ctrl1.sibling).toEqual(jasmine.any(SiblingController)); |
| 6340 | + expect(ctrl1.siblingOpt).toEqual(jasmine.any(SiblingOptController)); |
| 6341 | + |
| 6342 | + var ctrl2 = element.find('me').eq(1).controller('me'); |
| 6343 | + expect(ctrl2.parent).toEqual(jasmine.any(ParentController)); |
| 6344 | + expect(ctrl2.parentOpt).toBe(null); |
| 6345 | + expect(ctrl2.parentOrSibling1).toEqual(jasmine.any(ParentOrSiblingController)); |
| 6346 | + expect(ctrl2.parentOrSiblingOpt1).toBe(null); |
| 6347 | + expect(ctrl2.parentOrSibling2).toEqual(jasmine.any(ParentOrSiblingController)); |
| 6348 | + expect(ctrl2.parentOrSiblingOpt2).toBe(null); |
| 6349 | + expect(ctrl2.sibling).toEqual(jasmine.any(SiblingController)); |
| 6350 | + expect(ctrl2.siblingOpt).toBe(null); |
| 6351 | + }); |
| 6352 | + }); |
| 6353 | + |
6271 | 6354 |
|
6272 | 6355 | it('should not bind required controllers if bindToController is falsy', function() { |
6273 | 6356 | var parentController, siblingController; |
@@ -6797,6 +6880,31 @@ describe('$compile', function() { |
6797 | 6880 | }); |
6798 | 6881 | }); |
6799 | 6882 |
|
| 6883 | + it('should support omitting the name of the required controller if it is the same as the key', |
| 6884 | + function() { |
| 6885 | + module(function() { |
| 6886 | + directive('myC1', valueFn({ |
| 6887 | + controller: function() { this.name = 'c1'; } |
| 6888 | + })); |
| 6889 | + directive('myC2', valueFn({ |
| 6890 | + controller: function() { this.name = 'c2'; } |
| 6891 | + })); |
| 6892 | + directive('dep', function(log) { |
| 6893 | + return { |
| 6894 | + require: { myC1: '^', myC2: '^' }, |
| 6895 | + link: function(scope, element, attrs, controllers) { |
| 6896 | + log('dep:' + controllers.myC1.name + '-' + controllers.myC2.name); |
| 6897 | + } |
| 6898 | + }; |
| 6899 | + }); |
| 6900 | + }); |
| 6901 | + inject(function(log, $compile, $rootScope) { |
| 6902 | + element = $compile('<div my-c1 my-c2><div dep></div></div>')($rootScope); |
| 6903 | + expect(log).toEqual('dep:c1-c2'); |
| 6904 | + }); |
| 6905 | + } |
| 6906 | + ); |
| 6907 | + |
6800 | 6908 | it('should instantiate the controller just once when template/templateUrl', function() { |
6801 | 6909 | var syncCtrlSpy = jasmine.createSpy('sync controller'), |
6802 | 6910 | asyncCtrlSpy = jasmine.createSpy('async controller'); |
|