---
title: "How to use $location in AngularJS?"  
description: "How to use $location in AngularJS?"  
author: "Ashutosh Patel"  
published: 2024-06-19  
updated: 2024-06-19  
canonical: https://www.mindstick.com/interview/33920/how-to-use-location-in-angularjs  
category: "angular js"  
tags: ["javascript", "angular js", "angularjs service"]  
reading_time: 3 minutes  

---

# How to use $location in AngularJS?

#### $location in AngularJs

The $location function in AngularJS gives the browser access to URLs. It allows you to interact with the URL in the browser's address bar, such as accessing the current URL, editing the URL, or navigating to a different URL.

#### Basic Uses

Enter `$location` in your controller or service.

```javascript
angular.module('myApp').controller('MyController', ['$scope', '$location', function($scope, $location) {
   // Controller logic
}]);
```

**Accessing the current URL.**

```javascript
var currentUrl = $location.absUrl();
```

## Getting or Setting the Path

```javascript
var path = $location.path(); // Gets the current path (e.g., '/home')
$location.path('/newPath'); // Sets the path to '/newPath'
```

## Navigating to a Different URL

```javascript
$location.url('/newUrl'); // Changes the full URL to '/newUrl'
```

## Changing URL and Keep Browser History

```javascript
$location.path('/newPath').search({ param: 'value' });
```

## Reloading the current Route

```javascript
$location.path($location.path()).search($location.search());
```

- `$location` allows you to interact with URLs in AngularJS applications.
- Use `$location.path()` to get or set the path portion of the URL.
- Use `$location.url()` to set or get the full URL.

\
Important for **single page applications** (SPAs) where navigation takes place without full page redirection.

**Also, Read:** [How orderBy multiple fields in Angular?](https://www.mindstick.com/forum/160754/how-orderby-multiple-fields-in-angular)

## Answers

### Answer by Ashutosh Patel

#### $location in AngularJs

The $location function in AngularJS gives the browser access to URLs. It allows you to interact with the URL in the browser's address bar, such as accessing the current URL, editing the URL, or navigating to a different URL.

#### Basic Uses

Enter `$location` in your controller or service.

```javascript
angular.module('myApp').controller('MyController', ['$scope', '$location', function($scope, $location) {
   // Controller logic
}]);
```

**Accessing the current URL.**

```javascript
var currentUrl = $location.absUrl();
```

## Getting or Setting the Path

```javascript
var path = $location.path(); // Gets the current path (e.g., '/home')
$location.path('/newPath'); // Sets the path to '/newPath'
```

## Navigating to a Different URL

```javascript
$location.url('/newUrl'); // Changes the full URL to '/newUrl'
```

## Changing URL and Keep Browser History

```javascript
$location.path('/newPath').search({ param: 'value' });
```

## Reloading the current Route

```javascript
$location.path($location.path()).search($location.search());
```

- `$location` allows you to interact with URLs in AngularJS applications.
- Use `$location.path()` to get or set the path portion of the URL.
- Use `$location.url()` to set or get the full URL.

\
Important for **single page applications** (SPAs) where navigation takes place without full page redirection.

**Also, Read:** [How orderBy multiple fields in Angular?](https://www.mindstick.com/forum/160754/how-orderby-multiple-fields-in-angular)


---

Original Source: https://www.mindstick.com/interview/33920/how-to-use-location-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
