gpt4 book ai didi

javascript - 初学者的 AngularJS 路由

转载 作者:行者123 更新时间:2023-11-30 17:52:54 25 4
gpt4 key购买 nike

我正在尝试在我的 AngularJS 书籍 PDF 中重现路由示例。无法使其正常工作,我复制/粘贴了书中的代码以避免语法错误......但我不明白哪里出了问题。

当我加载 http://127.0.0.1:8080/routes/ 时,我应该看到“index.html”的内容和“inside”的 list.html 的内容。

但我只看到一个带有“A-Mail”的空白页面(index.html 的内容)

我已经使用 chrome 调试工具设置了一些断点,似乎我进入了 routeProvider 的“when('/routes/')”部分,但从未进入ListController 功能...我在控制台日志中没有错误

controllers.js

    // Creates a module for our core AMail services
var aMailServices = angular.module('AMail', []);

// Set up our mappings between URLs, templates, and controllers
function emailRouteConfig($routeProvider,$locationProvider){
$routeProvider.
when('/routes/', {
controller: ListController,
templateURL: 'list.html'
}).
// Notice that for the detail view, we specify a parameterized URL component
// by placing a colon in front of the id
when('/routes/view/:id', {
controller: DetailController,
templateURL: 'detail.html'
}).
otherwise({
redirectTo: '/routes/'
});
$locationProvider.html5Mode(true).hashPrefix('!');
}

// Set up our route so the AMail service can find it
aMailServices.config(emailRouteConfig)

// Some fake emails
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, we should get together for lunch sometime and catch up.'
+'There are many things we should collaborate on this year.'
}
];

// 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>
<meta charset="utf-8">
<title>Title...</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular.min.js"></script>
<script src="js/controllers.js"></script>
</head>
<body>
<h1>A-Mail</h1>
<div ng-view></div>
</body>
</html>

detail.html

<div><strong>Subject: </strong>{{message.subject}}</div>
<div><strong>Sender: </strong>{{message.sender}}</div>
<div><strong>Date: </strong>{{message.date}}</div>
<div>
<strong>To:</strong>
<span ng-repeat='recipient in message.recipients'>{{recipient}}</span>
<div>{{message.message}}</div>
<a href='#/'>Back to message list</a>
</div>

list.html

<table>
<tr>
<td><strong>Sender</strong></td>
<td><strong>Subject</strong></td>
<td><strong>Date</strong></td>
</tr>
<tr ng-repeat="message in messages">
<td>{{message.sender}}</td>
<td><a href="#/view/{{message.id}}">{{message.subject}}</a></td>
<td>{{message.date}}</td>
</tr>
</table>

谢谢

最佳答案

如你所见the offical document ,我认为您应该使用 templateUrl 而不是 templateURL

关于javascript - 初学者的 AngularJS 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609998/

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