AngularJS与路径参数直接将URL时在HTML5模式不加载路线 [英] AngularJS routes with parameters in path not loading in HTML5 mode when going directly to URLs

查看:221
本文介绍了AngularJS与路径参数直接将URL时在HTML5模式不加载路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有指定的几条路线:

app.config(["$routeProvider", "$locationProvider", function($routeProvider, $locationProvider) {
    $routeProvider.
    when("/", {
        templateUrl: "/ajax/home.html",
        controller: "HomeController"
    }).
    when("/test/:id", {
        templateUrl: "/ajax/test.html",
        controller: "TestController",
        resolve: {
            data: function ($q, $http, $route) {
                var deferred = $q.defer();
                var params = $route.current.params;

                $http({method: "GET", url: "/api/test/" + params.id + ".json"})
                    .success(function(data) {
                        deferred.resolve(data)
                    })
                    .error(function(data){
                        deferred.reject();
                    });

                return deferred.promise;
            }
        }
    });

    $locationProvider.html5Mode(true);

}]);

在上有通往 /测试/ X ,它工作正常另一条路线的链接。它还在不HTML5模式下正常工作。但是,当您直接导航到 /测试/ X 在HTML5模式,路线不加载并执行没有的东西在的决心。

When there is a link on another route leading to /test/x, it works fine. It also works fine when not in HTML5 mode. However, when you navigate directly to /test/x in HTML5 mode, the route doesn't load and none of the stuff in resolve is executed.

我已经通过许多AngularJS文档看了看,仍然无法想出解决办法。 PLZ:(

I've looked through much of the AngularJS documentation and still can't figure this out. plz :(

编辑:我做更多的测试,这是仅适用于在他们斜线路线。这似乎并不重要,如果有一个参数(如:ID )的与否。将 /你好(如果该路由定义)的作品在HTML5和非HTML5模式下,所有的案件。要像 /你好/世界始终工作在非HTML5模式,当路由是从另一路由通过单击链接而改变了HTML5模式下工作。在 /你好/世界时,令人耳目一新,将地址栏和pressing输入或点击从其他网站指向一个链接使其重新加载索引页,但不是实际的路线

I've done more testing, and this is only for routes that have a slash in them. It doesn't seem to matter if there is a parameter (like :id) in it or not. Going to /hello (if that route is defined) works for all cases in both HTML5 and non-HTML5 mode. Going to something like /hello/world always works in non-HTML5 mode and works in HTML5 mode when the route is changed from another route by clicking a link. Refreshing when on /hello/world, going to the address bar and pressing enter or clicking a link pointing to it from another website causes it to reload the index page but not the actual route.

推荐答案

添加<基本href =//> <头方式> 部分是一个解决方法,这取决于您遇到的问题

Adding <base href="/" /> to the <head> section is a fix, depending on which issue you are experiencing.

(这是我的答案,所以我想确保,使之更加明显对于遇到这种人)

(this was the answer for me, so I wanted to make sure to make it more obvious for others that run into this)

感谢@ user27766指点出来。

Thanks to @user27766 for pointing it out.

这篇关于AngularJS与路径参数直接将URL时在HTML5模式不加载路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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