与angularJS的$ routeProvider错误

时间:2014-04-26 13:16:47

标签: javascript angularjs ngroute

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

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

如果ngRoute服务内置于Angular中,为什么错误表明它未知?

<子>的 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: 'jean@somecompany.com', 
           subject: 'Hi there, old friend',
       date: 'Dec 7, 2013 12:32:00', 
           recipients: ['greg@somecompany.com'],
           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 个答案:

答案 0 :(得分:5)

您需要声明对ngRoute的依赖:

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