Angular 路线不工作
Angular route not working
我正在尝试在我的 Angular 应用程序中设置一些基本路线,但似乎 ngView
无法正常工作。下面是我的相关 index.html 代码、模块和配置:
index.html:
<body ng-app="myApp">
<h3>Testing is live</h3>
<div ng-view></div>
</body>
app.module.js:
angular.module('myApp', ['ngRoute', 'ngResource']);
app.config.js:
angular.module('myApp')
.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/player/:playerId',
{
template: '<h1>testing...</h1>',
controller: 'PlayerInfoController',
controllerAs: 'playerInfo'
}
);
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
});
例如,当我访问 /player/56
时,我看到 'Testing is live',但不是我期望的 'testing...'。相反,该部分中只有 <!-- ngView: -->
。
修复双重声明问题后,你能判断路由是否被命中了吗?当您导航到该路线时,是否会在您的 chrome 开发人员工具控制台中调用该 PlayerInfoController 中的 console.log?或者您是否收到任何类型的错误?
我对上面的代码做了两处修改以使其正常工作,但我不完全理解为什么这是必要的;这对我来说似乎是 "hacky" 修复。
在我的配置文件中:
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
被替换为:
$locationProvider.html5Mode(true);
并且我将以下内容添加到我的 index.html 文件的 head
中:
<base href="/player">
我正在尝试在我的 Angular 应用程序中设置一些基本路线,但似乎 ngView
无法正常工作。下面是我的相关 index.html 代码、模块和配置:
index.html:
<body ng-app="myApp">
<h3>Testing is live</h3>
<div ng-view></div>
</body>
app.module.js:
angular.module('myApp', ['ngRoute', 'ngResource']);
app.config.js:
angular.module('myApp')
.config(function($routeProvider, $locationProvider) {
$routeProvider.when('/player/:playerId',
{
template: '<h1>testing...</h1>',
controller: 'PlayerInfoController',
controllerAs: 'playerInfo'
}
);
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
});
例如,当我访问 /player/56
时,我看到 'Testing is live',但不是我期望的 'testing...'。相反,该部分中只有 <!-- ngView: -->
。
修复双重声明问题后,你能判断路由是否被命中了吗?当您导航到该路线时,是否会在您的 chrome 开发人员工具控制台中调用该 PlayerInfoController 中的 console.log?或者您是否收到任何类型的错误?
我对上面的代码做了两处修改以使其正常工作,但我不完全理解为什么这是必要的;这对我来说似乎是 "hacky" 修复。
在我的配置文件中:
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
被替换为:
$locationProvider.html5Mode(true);
并且我将以下内容添加到我的 index.html 文件的 head
中:
<base href="/player">