【问题标题】:angularjs pretty url is not workingangularjs漂亮的网址不起作用
【发布时间】:2014-07-31 16:54:38
【问题描述】:

我的问题是 url 没有 # 就不能工作。这是我的代码:

angular.module('Hiren', ['ngRoute'])
.config(function ($routeProvider, $locationProvider) {
    $routeProvider
        .when('/', {
            templateUrl: 'partials/login.html',
            controller: 'login'
        })
        .when('/redirect', {
            templateUrl: 'partials/postLogin.html',
            controller: 'postLogin'
        });

    $locationProvider.html5Mode(true);
});

当我尝试浏览 url example.com/redirect 时,它给了我 404 ,但使用哈希 (example.com/#redirect) 可以完美运行。

【问题讨论】:

    标签: angularjs angularjs-routing


    【解决方案1】:

    如果您在 HTML5 模式下遇到 404,您需要配置服务器以在服务器上发生 404 时为您的 Angular 应用程序(通常是 index.html 或 app.html)提供服务。你没有提到你正在使用什么服务器,所以我不能给出具体的说明如何做到这一点。这更像是一个服务器配置问题,而不是 Angular 问题。

    现在编辑服务器已知:

    import SimpleHTTPServer, SocketServer
    import urlparse, os
    
    PORT = 3000
    
    class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
       def do_GET(self):
    
           # Parse query data to find out what was requested
           parsedParams = urlparse.urlparse(self.path)
    
           # See if the file requested exists
           if os.access('.' + os.sep + parsedParams.path, os.R_OK):
              # File exists, serve it up
              SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);
           else:
              # redirect to index.html
              self.send_response(302)
              self.send_header('Content-Type', 'text/html')  
              self.send_header('location', '/index.html')  
              self.end_headers()
    
    Handler = MyHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

    Referenced SO answer.

    【讨论】:

    • 我正在使用python SimpleHttpServer进行开发。
    猜你喜欢
    • 2015-06-11
    • 2015-10-17
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-24
    • 2015-06-01
    • 1970-01-01
    相关资源
    最近更新 更多