Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$locationProvider.html5Mode(true) issues

been dealing with some $locationProvider issues that I am stuck on. I've got a simple single page page. But I'm getting the following error:

     TypeError: Cannot read property 'replace' of undefined
        at trimEmptyHash (angular.js:10551)
        at Object.$locationWatch (angular.js:11399)
        at Scope.$get.Scope.$digest (angular.js:14217)
        at Scope.$get.Scope.$apply (angular.js:14488)
        at bootstrapApply (angular.js:1449)
        at Object.invoke (angular.js:4182)
        at doBootstrap (angular.js:1447)
        at bootstrap (angular.js:1467)
        at angularInit (angular.js:1361)
        at HTMLDocument.<anonymous> (angular.js:26065)

My app.js file is pretty simple...

    var app = angular.module('app',
        [
            'ui.router'
        ]
    );

    app.config([
        '$stateProvider',
        '$httpProvider',
        '$locationProvider',
        '$urlRouterProvider',
        function ($stateProvider, $httpProvider, $locationProvider, $urlRouterProvider) {

            $locationProvider.hashPrefix('!');
            $locationProvider.html5Mode(true);

            $stateProvider
            .state('home',
            {
                url: '/',
                views: {
                    'aboutView':
                        {
                            template: function (params) {
                                console.log("home");
                                return 'home';
                            }
                        }
                }
            })
            .state('about',
            {
                url: '/about',
                views: {
                    'aboutView':
                        {
                            template: function (params) {
                                console.log('about')
                                return 'about';
                            }
                        }
                }
            })
            ;
        }
    ]);

I do have the <base href="http://localhost/apps/uiv8/" /> set in my index.html file. So, when I comment out the $locationProvider code, everything works fine in # mode. I can get to /#/about, etc. without issue. As soon as I put the $locationProvider parts back in, nothing.

A little bit more about my environment.... We do have asp.net's MVC in here, and the route.config is doing a {*url} to redirect all to the default route, and I've even gone as far as modifying IIS with url rewrites to send to the default, but I still get the parse Error above.

So, anybody got any ideas what's going on?

Thanks, Nick

like image 877
Nick Jacobs Avatar asked Mar 04 '15 16:03

Nick Jacobs


People also ask

How to support direct loading of HTML5 URLs using $locationprovider?

If you do no want to specify a base tag, configure $locationProvider to not require a base tag by passing a definition object with requireBase:false to $locationProvider.html5Mode () like this: In order to support direct loading of HTML5 URLs, you need to enabler server-side URL rewriting.

What is the default value for the prefix html5mode?

The default value for the prefix is '!'. If boolean, sets html5Mode.enabled to value. If object, sets enabled, requireBase and rewriteLinks to respective values.

Why am I getting an error when using $location with $location?

If enabled and requireBase are true, and a base tag is not present, an error will be thrown when $location is injected. See the $location guide for more information rewriteLinks - {boolean|string} - (default: true) When html5Mode is enabled, enables/disables URL rewriting for relative links.

How do I enable URL rewriting for relative links in html5mode?

rewriteLinks - {boolean|string} - (default: true) When html5Mode is enabled, enables/disables URL rewriting for relative links. If set to a string, URL rewriting will only happen on links with an attribute that matches the given string. For example, if set to 'internal-link', then the URL will only be rewritten for <a internal-link> links.


1 Answers

Add the base tag in your index page like :

<base href="/">

Instead of :

<base href="http://localhost/apps/uiv8/" />

Hope it will solve your problem.

like image 102
Mohan Raj Nagarajan Avatar answered Oct 05 '22 04:10

Mohan Raj Nagarajan