Javascript 切换到HTML5模式时,角度锚会断开

Javascript 切换到HTML5模式时,角度锚会断开,javascript,html,angularjs,Javascript,Html,Angularjs,我有一个恼人的问题,我不确定有没有解决办法; 我们在生产中运行HTML5模式,但不在开发中运行。原因是很难在spring boot的嵌入式tomcat上设置。我们对html锚进行如下编码: <a href="/route">Route</a> <a href="#" ng-click="goto('/route')">Route</a> 并且在$rootScope中具有全局函数: $scope.goto = function (route) {

我有一个恼人的问题,我不确定有没有解决办法; 我们在生产中运行HTML5模式,但不在开发中运行。原因是很难在spring boot的嵌入式tomcat上设置。我们对html锚进行如下编码:

<a href="/route">Route</a>
<a href="#" ng-click="goto('/route')">Route</a>
并且在$rootScope中具有全局函数:

$scope.goto = function (route) {
    $location.path(route);
}
但这似乎是错误的。我曾希望在锚点上使用ng href而不是href可以解决这个问题(也许可以检测我们是否处于HTML5模式,并自动将哈希添加到url,以便在切换模式时不需要重构整个应用程序),但事实并非如此


没有明确说明散列URL,就没有办法使用锚吗?

好的,我仍然认为这是angular中的一个设计缺陷,这个特定的修复应该存在于angular本身中

此指令直接与“href”属性匹配。我还没有做过任何关于这如何影响性能的测试,但它只在内部url上执行重写

angular.module(_DIRECTIVES_).directive('href', ['$location', function ($location) {
    'use strict';

    return {
        restrict:   'A',
        scope: {
            href: '@'
        },
        link: function (scope, element, attrs) {
            // Check if we need to rewrite the href
            var currentBase = $location.$$protocol + '://' + $location.$$host;
            var newBase     = element[0].href;
            if (element[0].nodeName == 'A' && newBase.indexOf(currentBase) == 0  && !$location.$$html5) {
                // Perform the href AFTER angular has done it's thing. Otherwise angular will rewrite back to the original
                scope.$watch('href', function () {
                    // This function may be run several times. Make sure the rewrite only happens once.
                    if (scope.href.indexOf('#!') !== 0) {
                        scope.href = '#!' + scope.href;
                        element.attr('href', scope.href);
                    }
                });
            }
        }
    };
}]);

我希望这能帮助任何和我一样陷入困境的人。也希望angular将来能解决这个问题。

@SergiuParaschiv这正是我想要避免的,也是当我们在HTML5模式和非HTML5模式之间切换时给我们带来问题的原因。如果你在最后读到这个问题,我会问你如何使用锚链接而不声明散列URL。看起来你自己在问题中给出了答案。您需要一个与ng href不同的指令,该指令考虑HTML5模式。当不是html5模式时,在URL前面连接一个#应该是直接的。。。我真的希望我只是在文档中遗漏了一些东西。如果在这样的模式之间切换时,角度不能正确处理路由,我会认为这是一个设计缺陷。应该可以在开发中使用html5mode=false,并在生产中轻松切换到html5mode=true,而无需替换应用程序中的所有HREF或创建自定义指令来“隐藏”缺陷。你不同意吗?