2014-04-26 56 views
2

我开始学习AngularJS今天,我坚持与“路由”部分。我创建了一个控制器和视图(见下文),但是当我尝试我的本地服务器上运行它,我得到以下错误:

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

如果ngRoute服务内置到角,为什么会出错提示它是未知的?

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> 

回答

5

您需要声明的ngRoute的依赖:

var aMailServices = angular.module('AMail', ['ngRoute']); 
+3

此外还需要在页面上包含'angular-route.js' – rossipedia

+0

但这不在书中。我从O'Rileys AngularJS学习。好的,谢谢你。我认为路线是建立在标准的Angular运输代码指导下的。 – user3283104

+1

@ user3283104它曾经是主库的一部分,但他们决定在1.0和1.2版本之间解耦它 - 如果你使用的是旧版本的书籍,你可能会发现这个链接很有用:https:// docs .angularjs.org /导向/迁移 – Emissary