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

Commit 4c8aeef

Browse files
gkalpakpetebacondarwin
authored andcommitted
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. Fixes #14391 Closes #14402
1 parent d384834 commit 4c8aeef

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-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: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10506,7 +10506,7 @@ describe('$compile', function() {
1050610506
});
1050710507

1050810508
it('should expose additional annotations on the directive definition object', function() {
10509-
var myModule = angular.module('my', []).component('myComponent', {
10509+
angular.module('my', []).component('myComponent', {
1051010510
$canActivate: 'canActivate',
1051110511
$routeConfig: 'routeConfig',
1051210512
$customAnnotation: 'XXX'
@@ -10522,7 +10522,7 @@ describe('$compile', function() {
1052210522
});
1052310523

1052410524
it('should support custom annotations if the controller is named', function() {
10525-
var myModule = angular.module('my', []).component('myComponent', {
10525+
angular.module('my', []).component('myComponent', {
1052610526
$customAnnotation: 'XXX',
1052710527
controller: 'SomeNamedController'
1052810528
});
@@ -10534,6 +10534,26 @@ describe('$compile', function() {
1053410534
});
1053510535
});
1053610536

10537+
it('should provide a new empty controller if none is specified', function() {
10538+
angular.
10539+
module('my', []).
10540+
component('myComponent1', {$customAnnotation1: 'XXX'}).
10541+
component('myComponent2', {$customAnnotation2: 'YYY'});
10542+
10543+
module('my');
10544+
10545+
inject(function(myComponent1Directive, myComponent2Directive) {
10546+
var ctrl1 = myComponent1Directive[0].controller;
10547+
var ctrl2 = myComponent2Directive[0].controller;
10548+
10549+
expect(ctrl1).not.toBe(ctrl2);
10550+
expect(ctrl1.$customAnnotation1).toBe('XXX');
10551+
expect(ctrl1.$customAnnotation2).toBeUndefined();
10552+
expect(ctrl2.$customAnnotation1).toBeUndefined();
10553+
expect(ctrl2.$customAnnotation2).toBe('YYY');
10554+
});
10555+
});
10556+
1053710557
it('should return ddo with reasonable defaults', function() {
1053810558
angular.module('my', []).component('myComponent', {});
1053910559
module('my');

0 commit comments

Comments
 (0)