Remove the numeric symbol from your root url in Angularjs - angularjs

Angular js appends the extenssion "/#" to my url (eg. http://example.com/#/) but I'd like my root direction to be completely clean (without the /#).
Is there a way to override this to have a clean home url?

Set html5Mode to true in your config via $locationProvider:
$locationProvider.html5Mode(true);
See: http://docs.angularjs.org/guide/dev_guide.services.$location

You need to configure the $location provider in order to get rid of the hash # in the url. This documentation goes through how to configure for html5 mode.
This SO Question will also help:
Removing the fragment identifier from AngularJS urls (# symbol)

I also faced this issue. This could be achieved by
Add $locationProvider.html5Mode(true) in the config
$locationProvider.html5Mode(true)
Add base url in the Index.html head tag
<base href="/app/index.html">

Related

How to remove index.html and exclamation sign(!) from URL using AngularJs ui-router?

My state configuration js
Now, my URL look like this
I want to remove index.html and (!) from my URL. Like as following image
How to do achieve this?
Consult the documentation for $location methods html5Mode and hashPrefix: https://docs.angularjs.org/guide/$location
$location.hashPrefix('');

AngularJS "#!" on url

someone knows what "#!" Means. on the url?
I was working until then this appears,I had a "#" always in the url and would like to keep it that way.
Currently routing is not working anymore,probably because of this url change.
It's part of a business project, i don`t want to change to html5Mode.
I tried to use:
$locationProvider.hashPrefix("");
Even correcting the url in this way routing is having problems
Thats called hash-bang.
To fix this use :
angular.module('yourApp', [])
.config(['$locationProvider', function($locationProvider) {
$locationProvider.hashPrefix('');
}]);
Adding html5Mode would even get rid of the '#' in the url but if you refresh the page, then you would get a 404 error. This can be fixed by configuring your server a bit. For that you might want to check the nice little tutorials :
https://scotch.io/tutorials/pretty-urls-in-angularjs-removing-the-hashtag
https://www.theodo.fr/blog/2017/01/pretty-url-in-angularjs-and-loopback-drop-the/
There is another answer to the hashbang issue in stackoverlow :
Doing links like Twitter, Hash-Bang #! URL's
You can use html5Mode in your location provider.
$locationProvider.html5Mode(true);
more info https://scotch.io/tutorials/pretty-urls-in-angularjs-removing-the-hashtag
Set html5mode true in your app.config
$locationProvider.html5Mode(true).hashPrefix('*');

issue in remove # from url in angularjs

remove # from url using
$locationProvider.html5Mode(true);
and display error
[$location:nobase] http://errors.angularjs.org/1.5.6/$location/nobase
It seems like you forgot to mention base to your html document.
$location in HTML5 mode requires a tag to be present! see documentation https://docs.angularjs.org/error/$location/nobase
<base href="/">

Configure angular ui-router to use absolute URLs by default

My angular app with ui-router is running inside a page at /content/page.html. The page also has a
<base href="/somewhere">
tag in the head.
Due to the base tag I end up with incorrect URLs pointing to /somewhere#/section1 instead of /content/page.html#/section1
I'm unable to remove this base tag since I'm just a small part of a large site. Is there a way to make ui-router generate absolute URLs by default? I've noticed that ui-sref-opts="{absolute: true}" can do this, but it generates the link using the base tag. Is there a way to make ui-router always create absolute URLs using window.location.pathname?
edit: I've created this plunker that demonstrates the problem.
Regards, Markus
You can try to replace <base href="/somewhere"> by <base href="/"> and add a link with your url in your config route in app.js.
html:
<base href='/'>
<a ui-sref="nameOfTheRoute">Link</a>
in app.js add the route:
$stateProvider.state('nameOfTheRoute',{url:'/', templateUrl:'Content/page.html'})
PS: I tried this on my application with another base href and it's works so you can try without change base url tag.
html:
<base href='/somewhere'>
<a ui-sref="nameOfTheRoute">Link</a>
in app.js add the route:
$stateProvider.state('nameOfTheRoute',{url:'/somewhere', templateUrl:'Content/page.html'})
I hope this will help you.
I hope this answer help you more than my previous answers ^^. I tested your plunker and now the default route works. I hope this is what you search
new plunker
Keep me informed.
Solved it by using an angular decorator and overriding the behaviour of the $urlRouter 'href' function.

AngularJS and PhoneGap: $location.path causes subsequent tempateUrl lookup to fail

I'm having trouble getting path lookup to work with a AngularJS v1.2.0 and PhoneGap/Cordova Android application. I've come pretty far with html5mode(true) by setting <base href="."/> in index.html and then changing $routeProvider.when('/') to $routeProvider.when('/android_asset/www/index.html'). After that I am able to get redirectTo('login') to reach $routeProvider.when('/login') and there render templateUrl: 'static/partials/login.html' as expected.
The problem I have is that if I instead try to redirect to the login page from my Javascript code with $location.path('/login');, the route is found but templateUrl loading fails with an insecurl exception.
I've tried whitelisting access to file:// by using the new angular-sanitize module, but that does not help.
How can I make $location.path() do the same things as redirectTo so that the partial is loaded? Or is there some other way to solve this problem?
UPDATE: I got a bit forward by adding a call to replace() after the path function, e.g.:
$location.path('/login').replace();
but that seems like a hack, and it still causes the templateUrl in the otherwise route to fail with the same exception.
Any ideas on what might be wrong? Is it that html5mode(true) just does not work at this moment with Phonegap and the only way to fix this is to set it to false and add hashtags to every path (like is done in the angular phonegap seed project)?
For future reference, this is how I managed to solve the problem:
AngularJS currently does not seem to support html5mode(true) inside a Cordova application because of the insecurl problem I reported. What I had to do is add
var h5m = (typeof html5Mode !== 'undefined') ? html5Mode : true;
$locationProvider.html5Mode(h5m);
which gives me the possibility to explicitly set html5Mode in the PhoneGap index.html with a global variable:
<script>
var html5Mode = false;
</script>
So now $location.path('/login') as well as redirectTo: 'login' works, but links in html files, don't. To get those working in PhoneGap, with html5Mode disabled, I had to add #/ in front of every link, e.g. login.
That makes PhoneGap work, but breaks the web page which uses History API with html5Mode(true). The last piece of the puzzle was to add <base href="/"/> to the web page's index.html (and leave it out of the index.html of the PhoneGap project.) So now even though I have a link that says #/login in the web page, I get to the url http://example.com/login and don't see any hashes in the address bar.
**
So in the end I have History API working in my web page and History API disabled in the PhoneGap project (where there really is no need for History API as there is no address bar). The only downside is the extra #/ I have to put in each template html file, but that is a minor annoyance compared to the ability to use all of the same html and javascript files for both web and mobile.
I had this same problem as well. I managed to fix it by skipping the leading slash in the route config:
$routeProvider
// route for the foo page
.when('/foo', {
templateUrl: 'foo.html', //previously: '/foo.html'
controller: 'fooController'
}) //etc.

Resources