Skip to content
Browse files

feat($controller): throw when requested controller is not registered

Previously, it would throw the ng:areq error, which is less
specific and just informs that the requested controller is not defined.
Given how commonly controllers are used
in Angular, it makes sense to have a specific error.

The ng:areq error is still thrown when the registered controller
is not a function.

Closes #14980
PR (#15015)
  • Loading branch information...
Narretz authored and petebacondarwin committed Aug 12, 2016
1 parent 7fa2fdb commit 9ae793d8a69afe84370b601e07fc375fc18a576a
@@ -0,0 +1,23 @@
@ngdoc error
@name $controller:ctrlreg
@fullName A controller with this name is not registered.

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, for example through the {@link ng.ngController `ngController`} directive,
or inside a {@link angular.Module#component component} / {@link angular.Module#directive directive} /
{@link ngRoute.$routeProvider#when route} definition (when using a string for the controller property).
Third-party modules can also instantiate controllers with the {@link ng.$controller `$controller()`} service.

Causes for this error can be:

1. Your reference to the controller has a typo. For example, in
the {@link ng.ngController `ngController`} directive attribute, in a {@link angular.Module#component component}
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.

Please consult the {@link ng.$controller $controller} service api docs to learn more.
@@ -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
@@ -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);

@@ -161,6 +161,12 @@ describe('$controller', function() {

it('should throw ctrlreg when the controller name does not match a registered controller', function() {
expect(function() {
$controller('IDoNotExist', {$scope: {}});
}).toThrowMinErr('$controller', 'ctrlreg', 'The controller with the name \'IDoNotExist\' is not registered.');

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

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

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

@@ -150,4 +150,5 @@ describe('ngController', function() {


0 comments on commit 9ae793d

Please sign in to comment.
You can’t perform that action at this time.