0

サーバーコード:

app.get('/', function(req, res){
    console.log('executed "/"')
     res.render('home');
});

app.get('/partials/:name', function (req, res) {
    console.log('executed partials:name');
    var name = req.params.name;
    console.log(name);
    res.render('partials/' + name);
});

$locationProvider.html5Mode(true);「/#/」URLを通常のURLに変換する前に、コードは完全に機能し/ます。

その後、console.log('executed partials:name');実行に失敗します。ドキュメンテーションは言う:-

Server side
Using this mode requires URL rewriting on server side, basically you have to rewrite all 
your links to entry point of your application (e.g. index.html)

私が試した変更は機能しません。どのような変更が必要ですか?

編集:以下はAngularコードです:

var myApp = angular.module('myApp', []);

myApp.config(['$routeProvider', function($routeProvider, $locationProvider) {
  $routeProvider.
      when('/', {
        templateUrl: 'partials/partial',
        controller: 'homePage'
      }).      
      otherwise({redirectTo: '/login'});
    $locationProvider.html5Mode(true);
}]);

$locationProvider.html5Mode(true);繰り返しますが、角度を追加するまで、ルーティングは完全にうまく機能します。

4

2 に答える 2

0

この場合、エラーはルーティングにはありません。myApp.config(['$routeProvider', function($routeProvider, $locationProvider)定義が間違っています。

このように含める必要があり$locationProviderます:-

myApp.config(['$routeProvider', '$locationProvider', function($routeProvider,
 $locationProvider) {

完璧に動作します。

于 2013-10-04T09:04:45.077 に答える
0

Not quite sure if this matches your problem but when we did this we needed to create a few rules on the server(in Nginx with our setup) to make sure a link to /page didn't just return 404 because it didn't exist on the server.

First we needed to make sure that a link to an Angular route was treated as such. So we do a rewrite ourdomain.com/page to ourdomain.com/#/page. /page doesn't exist on the server, only in Angular, so it would return 404. It needs to go to the index so that Angular can handle the route (this is relevant from incoming links, not so much for when you are on the site since then Angular handles the links anyway).

Secondly we needed to make an exceptions for partials, since those actually actually exists on the server and are not Angular routes. So going to ourdomain.com/partials/partial actually needs to find the partial on the server and can't be rewritten by the above rule.

Hope that helps somewhat at least.

于 2013-10-04T08:41:32.643 に答える