【问题标题】:Angularjs auto prefixes forward slashAngularjs自动前缀正斜杠
【发布时间】:2014-07-10 04:51:58
【问题描述】:

如果我点击网址说

www.xyz.com/home#route-1

AngularJS 自动将其重定向到

www.xyz.com/home#/route-1

也就是说 - 它在路由前加上 /(正斜杠)

为什么会这样,我怎样才能停止这样做?

更新 我真正要找的是 angular 不应该附加正斜杠也不删除井号。

【问题讨论】:

  • 你找到解决办法了吗?
  • @Bigood 查看下面的答案。
  • 您说您不想“删除井号”。 但是,这里指出的解决方案在我的情况下将其删除;这就是我问你的原因。

标签: javascript angularjs redirect


【解决方案1】:

@Tushar 我不确定您是否找到了解决方案,但我也遇到了您的情况,但谷歌搜索没有运气。最终我发现这是一个相当简单的修复,我补充道:-

angular.config(function($locationProvider) {
    $locationProvider.html5Mode({
        enabled: true,
        requireBase: false,
        rewriteLinks: false
    });
})

它只是停止将正斜杠 (/) 前缀附加到我的哈希锚。一切都保持我们熟悉的样子(没有用哈希或其他东西代替 URL)。

【讨论】:

  • 这是可行的,但是当我刷新页面时它显示“找不到对象!”页面任何解决方案
【解决方案2】:

如果你想在你的 AngularJS 应用中使用锚点,你应该使用$anchorScroll 服务。

"controller"

function ScrollCtrl($scope, $location, $anchorScroll) {
  $scope.gotoBottom = function (){
    // set the location.hash to the id of
    // the element you wish to scroll to.
    $location.hash('bottom');

    // call $anchorScroll()
    $anchorScroll();
  }
}

"html"

<div id="scrollArea" ng-controller="ScrollCtrl">
  <a ng-click="gotoBottom()">Go to bottom</a>
  <a id="bottom"></a> You're at the bottom!
</div>

You could see this working in AngularJS documentation.

如果你想美化 AngularJS 的 url,你可以启用 html5Mode:

.config(function($routeProvider, $locationProvider) {

    $routeProvider
        .when('/', {
            templateUrl : 'partials/home.html',
            controller : mainController
        })
        .when('/about', {
            templateUrl : 'partials/about.html',
            controller : mainController
        })
        .when('/contact', {
            templateUrl : 'partials/contact.html',
            controller : mainController
        });

    // use the HTML5 History API
    $locationProvider.html5Mode(true);
});

You could see more here

【讨论】:

  • 不,我与滚动页面无关。它只是变得混乱的简单路线。
  • 但这是 angularjs 路由的正常功能。我以为你有锚定问题。就我而言,我有带有 AngularJS 应用程序的 index.html,我可以从 localhost:9768/index.html#/loginlocalhost:9768/#/login 输入它
  • 你可以使用 html5Mode 来美化路线,我编辑了帖子
  • 有没有办法覆盖这种行为?
  • 是的,您可以使用 html5Mode 来美化路线,这是覆盖它的方法。路线将是 www.xyz.com/route-1。 $locationProvider.html5Mode(true);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-30
  • 1970-01-01
  • 1970-01-01
  • 2021-10-02
  • 1970-01-01
  • 2013-05-26
  • 2013-11-14
相关资源
最近更新 更多