如何从 angularjs ng-route 中删除哈希 # [英] How to remove the hash # from the angularjs ng-route

查看:22
本文介绍了如何从 angularjs ng-route 中删除哈希 #的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 locationProvider 从 angular js 中的 url 路由中删除主题标签,但它给了我错误.

I am trying to use the locationProvider to remove the hashtag from the url routes in angular js but it gives me error.

app.js

var eclassApp = angular.module('eclassApp', 
    ['ngRoute', 'eclassControllers', ]
);

eclassApp.config(['$routeProvider','$locationProvider',
    function ($routeProvider, $locationProvider){
        $routeProvider.
            when('/',{
                templateUrl: '/html/student-list.html',
                controller: 'StudentsListCtrl',
            }).
            when('/students/:studentId',{
                templateUrl: '/html/student-details.html',
                controller: 'StudentDetailsCtrl',

            }).otherwise({
                redirectTo: '/students'
            });
            $locationProvider.htmlMode(true);
    }]
);

错误:

 Uncaught Error: [$injector:modulerr] http://errors.angularjs.org/1.3.13/$injector/modulerr?p0=eclassApp&p1=TypeE…oogleapis.com%2Fajax%2Flibs%2Fangularjs%2F1.3.13%2Fangular.min.js%3A17%3A1)

我错过了什么吗?

使用这样的选项对象调用 html5Mode 函数

calling the html5Mode function with options object like this

$locationProvider.html5Mode({
    enabled:true
})

我收到以下错误(更改为 angular full 以获得对错误的更好解释,而不是缩小版本)

i get the following error (changed to angular full to get a better explanation of the error istead of the minified version)

Error: [$location:nobase] $location in HTML5 mode requires a <base> tag to be present!

http://errors.angularjs.org/1.3.13/$location/nobase

推荐答案

你可以像这样使用 $locationProvider -

you can use the $locationProvider like this -

$locationProvider.html5Mode({
  enabled: true,
  requireBase: false
});

或者,您可以在 index.html 中使用 base 标记(我想这是您的登录页面)

Alternatively, you can use the base tag in your index.html (I suppose this is your landing page)

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

删除 base 标签可能会在旧版 IE 浏览器(如 IE9)中产生一些副作用

Removing base tag may cause some side effects in old IE browser like IE9

这篇关于如何从 angularjs ng-route 中删除哈希 #的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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