Skip to content

Commit 9264cef

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 angular#14391 Closes angular#14402
1 parent a0b5e1a commit 9264cef

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
@@ -10538,7 +10538,7 @@ describe('$compile', function() {
1053810538
});
1053910539

1054010540
it('should expose additional annotations on the directive definition object', function() {
10541-
var myModule = angular.module('my', []).component('myComponent', {
10541+
angular.module('my', []).component('myComponent', {
1054210542
$canActivate: 'canActivate',
1054310543
$routeConfig: 'routeConfig',
1054410544
$customAnnotation: 'XXX'
@@ -10554,7 +10554,7 @@ describe('$compile', function() {
1055410554
});
1055510555

1055610556
it('should support custom annotations if the controller is named', function() {
10557-
var myModule = angular.module('my', []).component('myComponent', {
10557+
angular.module('my', []).component('myComponent', {
1055810558
$customAnnotation: 'XXX',
1055910559
controller: 'SomeNamedController'
1056010560
});
@@ -10566,6 +10566,26 @@ describe('$compile', function() {
1056610566
});
1056710567
});
1056810568

10569+
it('should provide a new empty controller if none is specified', function() {
10570+
angular.
10571+
module('my', []).
10572+
component('myComponent1', {$customAnnotation1: 'XXX'}).
10573+
component('myComponent2', {$customAnnotation2: 'YYY'});
10574+
10575+
module('my');
10576+
10577+
inject(function(myComponent1Directive, myComponent2Directive) {
10578+
var ctrl1 = myComponent1Directive[0].controller;
10579+
var ctrl2 = myComponent2Directive[0].controller;
10580+
10581+
expect(ctrl1).not.toBe(ctrl2);
10582+
expect(ctrl1.$customAnnotation1).toBe('XXX');
10583+
expect(ctrl1.$customAnnotation2).toBeUndefined();
10584+
expect(ctrl2.$customAnnotation1).toBeUndefined();
10585+
expect(ctrl2.$customAnnotation2).toBe('YYY');
10586+
});
10587+
});
10588+
1056910589
it('should return ddo with reasonable defaults', function() {
1057010590
angular.module('my', []).component('myComponent', {});
1057110591
module('my');

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy