2

I started learning AngularJS today and I am stuck with the "routing" part. I have created a controller and a view (see below) however when I try to run it on my local server I get the following error:

Uncaught Error: [$injector:modulerr] Failed to instantiate module AMail due to: Error: [$injector:unpr] Unknown provider: $routeProvider 

If the ngRoute service is built into Angular, why would the error suggest it's unknown?

controller.js

var aMailServices = angular.module('AMail', []); // Set up our mappings between URLs, templates, and controllers function emailRouteConfig($routeProvider) { $routeProvider. when('/', { controller: ListController, templateUrl: 'list.html' }). when('/view/:id', { controller: DetailController, templateUrl: 'detail.html' }). otherwise({ redirectTo: '/' }); } // Set up our route so the AMail service can find it aMailServices.config(emailRouteConfig); messages = [{ id: 0, sender: '[email protected]', subject: 'Hi there, old friend', date: 'Dec 7, 2013 12:32:00', recipients: ['[email protected]'], message: 'Hey' }]; // Publish our messages for the list template function ListController($scope) { $scope.messages = messages; } // Get the message id from the route (parsed from the URL) and use it to // find the right message object. function DetailController($scope, $routeParams) { $scope.message = messages[$routeParams.id]; } 

index.html

<html ng-app="AMail"> <head> </head> <body> <h1>A-Mail</h1> <div ng-view></div> <script src="angular.js"></script> <script src="controller.js"></script> </body> </html> 

1 Answer 1

5

You need to declare the dependency on ngRoute:

var aMailServices = angular.module('AMail', ['ngRoute']); 
Sign up to request clarification or add additional context in comments.

3 Comments

Also requires the angular-route.js to be included on the page
But that it not in the book. I am learning from O'Rileys AngularJS. Ok thanks for that. I thought route was built in directive with the standard Angular shipped code.
@user3283104 it used to be part of the main library but they decided to decouple it somewhere between versions 1.0 and 1.2 - you may find this link useful if you're working from an older book: docs.angularjs.org/guide/migration

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.