![]()
1. Understanding the Error
The error [$route:badroute] occurs when AngularJS fails to define or resolve a route properly. This usually happens due to:
- Incorrect route definitions in
$routeProvider. - Missing or incorrect template URLs.
- Controller issues in route configuration.
- Incorrect module dependencies or missing
ngRoute.
2. Common Causes & Fixes
Fix 1: Ensure ngRoute Module is Included
If ngRoute is not added, routing won’t work. Include it in your AngularJS app module:
var app = angular.module('myApp', ['ngRoute']);
- Without
ngRoute,$routeProviderwon’t be available.
Fix 2: Verify $routeProvider Configuration
Incorrect syntax or missing properties in route configuration can trigger $route:badroute.
Incorrect Route Example (Causes Error)
app.config(function($routeProvider) {
$routeProvider
.when('/home', { templateUrl: 'home.html' }) // Missing controller
.when('/about', { controller: 'AboutCtrl' }) // Missing template
.otherwise({ redirectTo: '/home' });
});
Corrected Route Configuration
app.config(function($routeProvider) {
$routeProvider
.when('/home', {
templateUrl: 'home.html',
controller: 'HomeCtrl'
})
.when('/about', {
templateUrl: 'about.html',
controller: 'AboutCtrl'
})
.otherwise({ redirectTo: '/home' });
});
- Each route must have both a
templateUrland acontroller.
Fix 3: Check Template Files Exist & Paths are Correct
If templateUrl points to a non-existent file, AngularJS cannot load the view.
- Ensure that home.html, about.html, etc., exist in the correct folder.
- Use relative paths if necessary:
templateUrl: './views/home.html' - Check case sensitivity in filenames, especially on Linux servers.
Fix 4: Verify Controllers Exist & Are Correct
If a controller referenced in $routeProvider does not exist, it triggers $route:badroute.
Example of Missing Controller (Causes Error)
app.config(function($routeProvider) {
$routeProvider.when('/contact', {
templateUrl: 'contact.html',
controller: 'ContactCtrl' // But 'ContactCtrl' is not defined!
});
});
Corrected Code
app.controller('ContactCtrl', function($scope) {
$scope.message = "Contact Us!";
});
- Make sure the controller exists before using it in
$routeProvider.
Fix 5: Use .otherwise() for Unmatched Routes
If no valid routes match, define a fallback with .otherwise().
$routeProvider.otherwise({ redirectTo: '/home' });
- This prevents invalid routes from breaking the app.
Fix 6: Debug Using $routeChangeError Event
To log detailed route errors, use $routeChangeError:
app.run(function($rootScope) {
$rootScope.$on("$routeChangeError", function(event, current, previous, rejection) {
console.error("Route Change Error:", rejection);
});
});
- This helps track issues like template load failures.
3. Summary & Best Practices
| Issue | Solution |
|---|---|
ngRoute not included | Add ['ngRoute'] in the module |
$routeProvider misconfiguration | Ensure templateUrl and controller exist |
| Template file missing | Check file paths and ensure they exist |
| Undefined controller | Define controllers before using them in routes |
| Invalid routes causing errors | Use $routeProvider.otherwise({ redirectTo: '/home' }) |
| Debugging required | Use $routeChangeError for logging |
