页面刷新时Angular'无法获取'路线

时间:2015-07-20 16:17:29

标签: javascript angularjs node.js

我的Angular应用程序中有一个搜索功能,当执行时会点击我的API以获取结果,然后使用$location.url重定向用户。一切都很好......直到我尝试重新加载结果页面。当我这样做时,我收到Cannot GET /search/[search-term]消息。

app.config.js:

'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);
    }
  });
};

2 个答案:

答案 0 :(得分:7)

设置您的快速应用:

app.route('/*').get(function(req, res) { 
    return res.sendFile(path.join(config.root, 'index.html')); 
});

答案 1 :(得分:0)

只是对Georgii Kats答案的一个小编辑。尝试使用:

 SELECT ContainerID, Master.SizeID, 
        Master.TypeID firstType,
        Type.TypeID secondType