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

feat($controller): throw error when requested controller is not regis… #15015

Merged
merged 4 commits into from
Aug 12, 2016
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 24 additions & 0 deletions docs/content/error/$controller/ctrlreg.ngdoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@ngdoc error
@name $controller:ctrlreg
@fullName A controller with this name is not registered.
@description

This error occurs when the {@link ng.$controller `$controller()`} service is called
with a string that does not match any of the registered controllers. The controller service may have
been invoked directly, or indirectly through the {@link ng.ngController `ngController`} directive,
Copy link
Member

Choose a reason for hiding this comment

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

I would add "for example through...", because the list is non-exhaustive.
(E.g. a route definitions for ngRoute or ui.router and possibly ngMaterial instantiate controllers as well under the hood.)

or when inside a {@link angular.Module#component component} / {@link angular.Module#directive directive}
definition (when using string notation for the controller property).

Sources for this error can be:

1. You have a typo in the {@link ng.ngController `ngController`} directive,
in a {@link angular.Module#component component} / {@link angular.Module#directive directive}
definition's controller property, or in the call to {@link ng.$controller `$controller()`}.
2. You have not registered the controller (neither via {@link angular.Module#controller `Module.controller`}
nor {@link ng.$controllerProvider#register `$controllerProvider.register()`}.
3. You have a typo in the *registered* controller name.
4. You want to use controllers defined on the `window`, but have turned off
Copy link
Member

Choose a reason for hiding this comment

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

I would mention that this feature is deprecated/not recommened.
E.g. something like [...] defined onwindow(a feature which is deprecated and turned off by default), but [...].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think then it's better to remove this point completely.

{@link ng.$controllerProvider#allowGlobals `allowGlobals()`}.


Please consult the {@link ng.$controller $controller} service api docs to learn more.
3 changes: 2 additions & 1 deletion docs/content/error/ng/areq.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

AngularJS often asserts that certain values will be present and truthy using a
helper function. If the assertion fails, this error is thrown. To fix this problem,
make sure that the value the assertion expects is defined and truthy.
make sure that the value the assertion expects is defined and matches the type mentioned in the
error.
2 changes: 1 addition & 1 deletion karma-shared.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ module.exports = function(config, specificOptions) {

config.logLevel = config.LOG_DEBUG;
// Karma (with socket.io 1.x) buffers by 50 and 50 tests can take a long time on IEs;-)
config.browserNoActivityTimeout = 120000;
config.browserNoActivityTimeout = 140000;

config.browserStack.build = buildLabel;
config.browserStack.startTunnel = false;
Expand Down
5 changes: 5 additions & 0 deletions src/ng/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ function $ControllerProvider() {
: getter(locals.$scope, constructor, true) ||
(globals ? getter($window, constructor, true) : undefined);

if (!expression) {
throw $controllerMinErr('ctrlreg',
'The controller with the name \'{0}\' is not registered.', constructor);
}

assertArgFn(expression, constructor, true);
}

Expand Down
7 changes: 6 additions & 1 deletion test/ng/controllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ describe('$controller', function() {
}).toThrow();
}));

it('should throw ctrlreg when the controller name does not match a registered controller', inject(function($compile, $rootScope) {
Copy link
Member

Choose a reason for hiding this comment

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

Unused injections.

expect(function() {
$controller('IDoNotExist', {$scope: {}});
}).toThrowMinErr('$controller', 'ctrlreg', 'The controller with the name \'IDoNotExist\' is not registered.');
}));


describe('ctrl as syntax', function() {

Expand Down Expand Up @@ -227,7 +233,6 @@ describe('$controller', function() {
'Must match `__name__ as __id__` or `__name__`.');
});


it('should allow identifiers containing `$`', function() {
var scope = {};

Expand Down
9 changes: 9 additions & 0 deletions test/ng/directive/ngControllerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,13 @@ describe('ngController', function() {
$httpBackend.flush();
expect(controllerScope.name).toBeUndefined();
}));

it('should throw ctrlreg when the controller name does not match a registered controller', inject(function($compile, $rootScope) {
element = jqLite('<div ng-controller="IDoNotExist"></div>');

expect(function() {
element = $compile(element)($rootScope);
}).toThrowMinErr('$controller', 'ctrlreg', 'The controller with the name \'IDoNotExist\' is not registered.');
}));

});