Expand Minimize

Route refers to a known controller

Route refers to a controller registered with the main module or one of its dependent modules

CheckId NG1010801
TypeName RouteRefersToAKnownController
Severity CriticalError
Type Route

When defining routes, you can specify a controller that should be associated with the scope created for the route using the controller property. You can specify the controller either as a function or by referring to an existing controller using its name. The name reference must point to a valid controller registered with a module known to the application.

Good practice
Example 1
HomeController referenced by the route is defined in the main application module.

function HomeController() {
}

angular.module('app')
    .controller('HomeController', HomeController)
    .config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: './spa/app/home/home.html',
      controller: 'HomeController',
            controllerAs: 'vm'
    })
    .when('/:id', {
      templateUrl: './spa/app/detail/detail.html',
      controller: 'DetailController',
            controllerAs: 'vm'
    })
    .otherwise({
      redirectTo: '/'
    });
}]);


Example 2
HomeController referenced by the route is defined in a module defined as a dependency of the main module.
function HomeController() {
}

angular.module('app.pages', [])
    .controller('HomeController', HomeController);

angular.module('app', ['app.pages'])
    .config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: './spa/app/home/home.html',
      controller: 'HomeController',
            controllerAs: 'vm'
    })
    .when('/:id', {
      templateUrl: './spa/app/detail/detail.html',
      controller: 'DetailController',
            controllerAs: 'vm'
    })
    .otherwise({
      redirectTo: '/'
    });
}]);


Bad practice
HomeController referenced by the route is defined in a module that isn't used by the main application module.
function HomeController() {
}

angular.module('app.pages', [])
    .controller('HomeController', HomeController);

angular.module('app', [])
    .config(['$routeProvider', function($routeProvider) {
  $routeProvider
    .when('/', {
      templateUrl: './spa/app/home/home.html',
      controller: 'HomeController',
            controllerAs: 'vm'
    })
    .when('/:id', {
      templateUrl: './spa/app/detail/detail.html',
      controller: 'DetailController',
            controllerAs: 'vm'
    })
    .otherwise({
      redirectTo: '/'
    });
}]);

Disclaimer: The views and opinions expressed in this documentation and in SPCAF do not necessarily reflect the opinions and recommendations of Microsoft or any member of Microsoft. SPCAF and RENCORE are registered trademarks of Rencore. All other trademarks, service marks, collective marks, copyrights, registered names, and marks used or cited by this documentation are the property of their respective owners.