Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit 1b4b1a2

Browse files
committed
fix($compile): do not use noop() as controller for multiple components
Currently, custom annotations are copied from the CDO onto the controller constructor. Using `noop()` when no controller has been specified, pollutes it with custom annotations and makes one component's annotations available to all other components that have `noop()` as their controller.
1 parent 0348347 commit 1b4b1a2

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/ng/compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
10921092
* See also {@link ng.$compileProvider#directive $compileProvider.directive()}.
10931093
*/
10941094
this.component = function registerComponent(name, options) {
1095-
var controller = options.controller || noop;
1095+
var controller = options.controller || function() {};
10961096

10971097
function factory($injector) {
10981098
function makeInjectable(fn) {

test/ng/compileSpec.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10494,7 +10494,7 @@ describe('$compile', function() {
1049410494
});
1049510495

1049610496
it('should expose additional annotations on the directive definition object', function() {
10497-
var myModule = angular.module('my', []).component('myComponent', {
10497+
angular.module('my', []).component('myComponent', {
1049810498
$canActivate: 'canActivate',
1049910499
$routeConfig: 'routeConfig',
1050010500
$customAnnotation: 'XXX'
@@ -10510,7 +10510,7 @@ describe('$compile', function() {
1051010510
});
1051110511

1051210512
it('should support custom annotations if the controller is named', function() {
10513-
var myModule = angular.module('my', []).component('myComponent', {
10513+
angular.module('my', []).component('myComponent', {
1051410514
$customAnnotation: 'XXX',
1051510515
controller: 'SomeNamedController'
1051610516
});
@@ -10522,6 +10522,24 @@ describe('$compile', function() {
1052210522
});
1052310523
});
1052410524

10525+
it('should provide a new empty controller if none is specified', function() {
10526+
angular.
10527+
module('my', []).
10528+
component('myComponent1', {$customAnnotation1: 'XXX'}).
10529+
component('myComponent2', {$customAnnotation2: 'YYY'});
10530+
module('my');
10531+
inject(function(myComponent1Directive, myComponent2Directive) {
10532+
var ctrl1 = myComponent1Directive[0].controller;
10533+
var ctrl2 = myComponent2Directive[0].controller;
10534+
10535+
expect(ctrl1).not.toBe(ctrl2);
10536+
expect(ctrl1.$customAnnotation1).toBe('XXX');
10537+
expect(ctrl1.$customAnnotation2).toBeUndefined();
10538+
expect(ctrl2.$customAnnotation1).toBeUndefined();
10539+
expect(ctrl2.$customAnnotation2).toBe('YYY');
10540+
});
10541+
});
10542+
1052510543
it('should return ddo with reasonable defaults', function() {
1052610544
angular.module('my', []).component('myComponent', {});
1052710545
module('my');

0 commit comments

Comments
 (0)