【问题标题】:Remove # symbol from the url in angular js从角度js中的url中删除#符号
【发布时间】:2017-01-03 12:10:09
【问题描述】:

我遇到了“#”符号的问题。我想从我的 Angular js 网站的 URL 中删除 # 。我尝试在“提供的链接”的帮助下将其删除,但它不起作用,并且该网站没有显示任何内容。请让我知道如何使它成为可能。如何从 URL 中删除 # 符号。

这是我的 route.js 代码:-

var app = angular.module("myApp", ["ngRoute"]);
app.config(function($routeProvider) {
    $routeProvider
    .when("/", {
        templateUrl: 'Templates/home.html',
    })
    .when("/first", {
        templateUrl: 'Templates/first.html',
    })
    .when("/second", {
        templateUrl: 'Templates/second.html',
    })
    .when("/third", {
        templateUrl: 'Templates/third.html',
    })
    .when("/admin", {
        templateUrl: 'Templates/admin.html',
    })
    .otherwise({
        redirectTo: '/'
    });

【问题讨论】:

  • 您尝试删除的内容 #。解释一下。

标签: javascript angularjs routing


【解决方案1】:

您可以使用$locationProvider

 var app = angular.module("myApp", ["ngRoute"]);
    app.config(function($routeProvider, $locationProvider) {
        $routeProvider
        .when("/", {
            templateUrl: 'Templates/home.html',
        })
        .when("/first", {
            templateUrl: 'Templates/first.html',
        })
        .when("/second", {
            templateUrl: 'Templates/second.html',
        })
        .when("/third", {
            templateUrl: 'Templates/third.html',
        })
        .when("/admin", {
            templateUrl: 'Templates/admin.html',
        })
        .otherwise({
            redirectTo: '/'
        });
             $locationProvider.html5Mode({
          enabled: true,
          requireBase: false
        });

【讨论】:

    【解决方案2】:

    使用 HTML5 历史 API

    $locationProvider.html5Mode(true);
    

    或者,您可以在 index.html 中使用基本标记(我想这是您的目标网页)

    <head>
      <base href="/">
    </head>
    

    更多详情,请访问:https://scotch.io/tutorials/pretty-urls-in-angularjs-removing-the-hashtag

    【讨论】:

    • 它对我不起作用。我在这里写之前试过了。我是 Angular js 的新手,所以我不知道我哪里出错了..!!
    【解决方案3】:

    在你的 app.js 中试试这个

        var app = angular.module("myApp", ["ngRoute"]);
    app.config('$routerProvider', '$locationProvider',function($routeProvider,$locationProvider) {
    
        $locationProvider.html5Mode(true).hashPrefix('*');
    
        $routeProvider
        .when("/", {
            templateUrl: 'Templates/home.html',
        })
        .when("/first", {
            templateUrl: 'Templates/first.html',
        })
        .when("/second", {
            templateUrl: 'Templates/second.html',
        })
        .when("/third", {
            templateUrl: 'Templates/third.html',
        })
        .when("/admin", {
            templateUrl: 'Templates/admin.html',
        })
        .otherwise({
            redirectTo: '/'
        });
    

    并在 index.html 文件的 head 部分中添加此基本标记

    <head>
       <base href="/">
    </head>
    

    【讨论】:

    • 不适合我你能详细解释一下吗??我做了上面提到的相同但没有工作..!!
    猜你喜欢
    • 2019-05-23
    • 1970-01-01
    • 2018-10-22
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2012-11-27
    相关资源
    最近更新 更多