如何从angularjs纳克路由删除散列# [英] How to remove the hash # from the angularjs ng-route

查看:251
本文介绍了如何从angularjs纳克路由删除散列#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用locationProvider从在角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)

我缺少的东西吗?

Am I missing something?

编辑:调用html5Mode功能与选项对象像这样

calling the html5Mode function with options object like this

$locationProvider.html5Mode({
    enabled:true
})

我收到以下错误(改为角度充分获得的缩小的版本错误istead的一个更好的解释)

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!

<一个href=\"http://errors.angularjs.org/1.3.13/$location/nobase\">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(我想这是您的目标网页)

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

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

删除基础标签可能会导致老的IE浏览器IE9一样有些副作用

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

这篇关于如何从angularjs纳克路由删除散列#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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