2014-04-26 74 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