我试图在我的 Angular 应用程序中设置一些基本路线,但似乎无法获得 ngView
才能正常工作。下面是我相关的 index.html 代码、模块和配置:
index.html:
<body ng-app="myApp">
<h3>Testing is live</h3>
<div ng-view></div>
</body>
应用程序模块.js:
angular.module('myApp', ['ngRoute', 'ngResource']);
应用程序配置文件:
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
时例如,我看到“正在测试”,而不是我所期望的“正在测试...”。相反,只有 <!-- ngView: -->
在那个部分。
最佳答案
我对上面的代码做了两处修改以使其工作,但我不完全理解为什么这是必要的;对我来说这似乎是一个“hacky”修复。
在我的配置文件中:
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
被替换为:
$locationProvider.html5Mode(true);
我将以下内容添加到我的 index.html 文件的 head
中:
<base href="/player">
关于javascript - Angular 路线不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31119231/