Pravin Pravin - 1 month ago 17x
AngularJS Question

Cannot get current route. $location.path() returns empty.

I am currently at

, and I want current route I am at. Here is my controller:

app.controller('DocRegistrationController',[ '$http', '$scope', '$upload', '$location', function($http, $scope, $upload, $location){

However it returns empty. I dont want to hardcode all the routes. What am I doing wrong?


$location service respond for parsing url in browser address bar and make the URL available to your APP.

Because you're using regular URL path and search segments, you have to set $locationProvider html5Mode to true.

$locationProvider will use hashbang as default mode.

If you don't set html5Mode to true, you will get empty string when you try to fetch url path.

Then use $location service to fetch the url path after set html5Mode.

And write your own rule to process the path.

Assume that your Full URL looks like:



function MainController($location){
    var pId = $location.path().split("/")[3]||"Unknown";    //path will be /person/show/321/, and array looks like: ["","person","show","321",""]


<!DOCTYPE html>
<html lang="en" ng-app="MyAPP">
        <meta charset="utf-8">
        <title>Angular test</title>
    <body ng-controller="MainController">
        <script src="//"></script>
        <script src="js/main.js"></script>

Hope this is helpful for you.