【发布时间】:2014-12-10 07:24:45
【问题描述】:
ngRoute 不工作,但没有向控制台报告错误。
如果控制台没有错误,如何跟踪 ngRoute 程序的执行?
我看到了使用 $locationProvider.html5Mode(true) 的示例,我不明白什么时候应该使用它,但我认为它不需要让 ngRoute 工作。
index.html 有导航链接和 ngView :
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<script src="bower_components/angular/angular.js"> </script>
<script src="bower_components/angular-route/angular-route.js"> </script>
<script src="main.js"> </script>
</head>
<body ng-app="Main">
<ul>
<li> <a href="#content/first"> first partial </a> </li>
<li> <a href="#content/second"> second partial </a> </li>
</ul>
<div ng-view></div>
</body>
</html>
main.js 定义了路由器和控制器:
var Main = angular.module('Main', ['ngRoute']);
function router($routeProvider) {
var route = {templateUrl: 'partials/default.html'};
$routeProvider.when('', route);
route = {
templateUrl: 'partials/first.html',
controller: 'first'
};
$routeProvider.when('content/first', route);
route = {
templateUrl: 'partials/second.html',
controller: 'second'
};
$routeProvider.when('content/second', route);
}
Main.config(['$routeProvider', router]);
Main.controller('first', function($scope) {
$scope.list = [1,2,3,4,5];
});
Main.controller('second', function($scope) {
$scope.list = [1,2,3];
});
partials 只是使用 ngRepeat:
<header> First content </header>
<p ng-repeat="iter in list">
first
</p>
已解决: 我的问题是我的整个应用程序位于 /ang/ 前缀下,并且在将该前缀添加到 url 之后,它现在正在工作。 不应该有使用相对网址的方法吗?我想应该有,我会尝试修复它。
问题不在于每个人建议的不同语法,这令人震惊,许多 JS 开发人员实际上并不了解他们在任何地方使用的单行语法。
【问题讨论】:
-
<a href="#/content/first">这样检查 -
不,使用 #/content/first 没有成功。
-
我看不到您在 html 中调用控制器的位置...
-
不应该在 html 中调用控制器,因为它是使用 $routeProvider "when" 子句完成的。
-
你误用了
$routeProvider,为什么不直接关注文档?