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

fix($compile): do not use noop() as controller for multiple components #14402

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/ng/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -1092,7 +1092,7 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
* See also {@link ng.$compileProvider#directive $compileProvider.directive()}.
*/
this.component = function registerComponent(name, options) {
var controller = options.controller || noop;
var controller = options.controller || function() {};

function factory($injector) {
function makeInjectable(fn) {
Expand Down
22 changes: 20 additions & 2 deletions test/ng/compileSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10494,7 +10494,7 @@ describe('$compile', function() {
});

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

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

it('should provide a new empty controller if none is specified', function() {
angular.
module('my', []).
component('myComponent1', {$customAnnotation1: 'XXX'}).
component('myComponent2', {$customAnnotation2: 'YYY'});
module('my');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There should be a newline before and after this line for readability.

inject(function(myComponent1Directive, myComponent2Directive) {
var ctrl1 = myComponent1Directive[0].controller;
var ctrl2 = myComponent2Directive[0].controller;

expect(ctrl1).not.toBe(ctrl2);
expect(ctrl1.$customAnnotation1).toBe('XXX');
expect(ctrl1.$customAnnotation2).toBeUndefined();
expect(ctrl2.$customAnnotation1).toBeUndefined();
expect(ctrl2.$customAnnotation2).toBe('YYY');
});
});

it('should return ddo with reasonable defaults', function() {
angular.module('my', []).component('myComponent', {});
module('my');
Expand Down