angularjs 漂亮的 url 不起作用 [英] angularjs pretty url is not working

查看:27
本文介绍了angularjs 漂亮的 url 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is url is not working without # . Here is my code :

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)它完美地工作.

When I try to browse the url example.com/redirect , it gives me 404 , but using hash ( example.com/#redirect) its perfectly working .

推荐答案

如果您在 HTML5 模式下遇到 404,您需要配置您的服务器以在以下情况下为您的 Angular 应用程序(通常是 index.html 或 app.html)提供服务404 发生在服务器上.您没有提及您用于服务器的内容,因此我无法提供有关如何执行此操作的具体说明.这与其说是 Angular 问题,不如说是一个服务器配置问题.

If you're getting a 404 in HTML5 mode you need to configure your server to serve your Angular application (usually index.html or app.html) when a 404 happens on the server. You don't mention what you're using for a server so I can't give specific instructions on how to do that. This is more a server configuration issue than an Angular one.

现在编辑服务器已知:

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()

参考 SO 答案.

这篇关于angularjs 漂亮的 url 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆