gpt4 book ai didi

javascript - angularJS 的 $routeProvider 错误

转载 作者:行者123 更新时间:2023-11-29 17:07:44 25 4
gpt4 key购买 nike

我今天开始学习 AngularJS,但我被“路由” 部分困住了。我已经创建了一个 Controller 和一个 View (见下文),但是当我尝试在本地服务器上运行它时,出现以下错误:

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

如果 Angular 中内置了 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: '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>

最佳答案

需要声明对ngRoute的依赖:

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

关于javascript - angularJS 的 $routeProvider 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23311429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com