如何删除 angular-ui-router URL 中的“#"符号 [英] How to delete '#' sign in angular-ui-router URLs

查看:23
本文介绍了如何删除 angular-ui-router URL 中的“#"符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 angular-ui-router 库,但 URL 有问题.

I'm using the angular-ui-router library and I have a problem with URLs.

我有以下代码:

app.js:

app.config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
    .state('state', {
        url: '/state',
        templateUrl: 'templates/state.html',
        onEnter: function () {
            /*... code ...*/
        }
    })});

index.html:

index.html:

<a href="#/state">STATE</a>

这有效,但是当我从 <a> 标签中删除#"时,这不起作用.

This works, but when I remove '#' from the <a> tag this doesn't work.

如何从 URL 中删除#"号?

How can I delete the '#' sign from the URL?

推荐答案

如果你想要不带哈希标签的导航,你需要启用 HTML5Mode:

You need to enable HTML5Mode if you want navigation without hash tags:

app.config(["$locationProvider", function($locationProvider) {
  $locationProvider.html5Mode(true);
}]);

您还需要通过将以下代码添加到 HTML 文件的 <head> 来告诉 angular 应用的根 URL:

You will also need to tell angular the root URL of your app by adding the following code to the <head> of your HTML file:

<base href="/">

请注意,对 HTML5 模式的支持取决于浏览器.对于那些不支持 History API 的人,Angular 将回退到 hashbang.

Be aware that support for HTML5 mode depends on the browser. For those who don't support the History API, Angular will fallback to hashbang.

这篇关于如何删除 angular-ui-router URL 中的“#"符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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