2015-07-20 55 views
2

我在我的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); 
    } 
    }); 
}; 
+0

您確定服務器響應index.html當您嘗試加載特定頁面嗎?嘗試關閉html5模式並檢查它。 – 2015-07-20 16:31:20

回答

6

設置您的明確程序:

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

我收到錯誤消息,說配置沒有定義。任何幫助? –

0

只是一個小編輯到格奧爾基·卡茨回答。嘗試使用:

app.get('/*', function (req, res, next) { 
     if (/.js|.html|.css|templates|js|scripts/.test(req.path) || req.xhr) { 
     return next({ status: 404, message: 'Not Found' }); 
     } else { 
      return res.render('index'); 
     } 
    });