我的 Angular 应用程序中有一个搜索功能,当执行时点击我的 API 以获取结果,然后使用 $location.url
重定向用户。一切都很好......直到我尝试重新加载结果页面。当我这样做时,我收到一条 Cannot GET/search/[search-term]
消息。
应用程序配置文件:
'use strict';
angular.module('gameApp')
.config(function($routeProvider, $locationProvider) {
$routeProvider
.when('/player/:playerId', {
templateUrl: 'player.html'
})
.when('/search/:searchTerm', {
templateUrl: '/app/gmSearchResults/gmSearchResults.html'
});
$locationProvider.html5Mode({
enabled: true,
requireBase: false,
rewriteLinks: false
});
});
相关的navbar.controller.js:
vm.getSearchResults = function(searchTerm) {
searchService.resource.getResults({
query: searchTerm
}).$promise.then(function(results) {
if (results.playerId) {
$location.url('/customer/' + results.playerId);
} else {
saveLatest(results);
$location.url('/search/' + searchTerm);
}
});
};
最佳答案
设置您的 Express 应用:
app.route('/*').get(function(req, res) {
return res.sendFile(path.join(config.root, 'index.html'));
});
关于javascript - 页面刷新时的 Angular 'Cannot GET' 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31521443/