AngularJS $locationProvider (original) (raw)
Last Updated : 29 Jan, 2023
The $locationProvider facilitates the configuration of the application by implementing the deep linking paths that are stored. Here are some of the things that can be made with the $locationProvider service:
- Set the html5Mode property to true to enable HTML5 mode, which uses the history.pushState API to change the URL without reloading the page.
- Set the hashPrefix property to a string to specify a prefix for the hash part of the URL (e.g. #!). This is used when HTML5 mode is not enabled.
- Set the hashPrefix to '!' to enable the "hashbang" mode, which is used by some search engines to index single-page applications.
Syntax:
angular.module('myApp', []) .config(function($locationProvider) { $locationProvider.html5Mode(true); });
Here are some of the functions available on the $locationProvider service:
- html5Mode(mode): Enables or disables HTML5 mode. When HTML5 mode is enabled, the locationserviceusesthehistory.pushStateAPItochangetheURLwithoutreloadingthepage.WhenHTML5modeisdisabled,thelocation service uses the history.pushState API to change the URL without reloading the page. When HTML5 mode is disabled, the locationserviceusesthehistory.pushStateAPItochangetheURLwithoutreloadingthepage.WhenHTML5modeisdisabled,thelocation service uses the hash part of the URL (e.g. #/path) to represent the current URL.
- hashPrefix(prefix): Sets the prefix for the hash part of the URL (e.g. #!). This is used when HTML5 mode is not enabled**.**
- baseHref(href): Sets the base URL for the application. This is used to resolve relative URLs.
Here are the different functions of the locationserviceinAngularJSthatisconfiguredbythelocation service in AngularJS that is configured by the locationserviceinAngularJSthatisconfiguredbythelocationProvider service:
- absUrl(): Returns the full URL of the current page, including the protocol (http or https), domain, and any query string parameters.
- path(): Returns the current path of the URL.
- hash(): Returns the current hash fragment of the URL.
- search(): Returns the current search parameters of the URL as an object.
- url(url): Sets the URL of the current page. If a URL is provided, it sets the URL to the provided value. If no value is provided, it returns the current URL.
- path(path): Sets the path of the current URL. If a path is provided, it sets the path to the provided value. If no value is provided, it returns the current path.
- hash(hash): Sets the hash fragment of the current URL. If a hash fragment is provided, it sets the hash fragment to the provided value. If no value is provided, it returns the current hash fragment.
- search(search): Sets the search parameters of the current URL. If search parameters are provided, it sets the search parameters to the provided value. If no value is provided, it returns the current search parameters.
Example 1: In this example, the MainCtrl controller uses the locationservicetoreadthecurrentURLandstoreitinthecurrentUrlscopevariable.ThecurrentURListhendisplayedinthepageusingAngularJS′sexpressionsyntax().Whenthepageisloaded,thelocation service to read the current URL and store it in the currentUrl scope variable. The current URL is then displayed in the page using AngularJS's expression syntax ({{}}). When the page is loaded, the locationservicetoreadthecurrentURLandstoreitinthecurrentUrlscopevariable.ThecurrentURListhendisplayedinthepageusingAngularJS′sexpressionsyntax().Whenthepageisloaded,thelocation service will read the current URL from the browser's address bar and make it available to the application. The absUrl method returns the full URL, including the protocol (http or https), domain, and any query string parameters.
HTML `
GeeksforGeeks
Current URL: {{currentUrl}}`
Output:
Example 2: In this example, we have a simple page with two links that change the URL when clicked. When the user clicks on one of the links, the changeLocation function is called, which uses the locationservicetochangetheURLbysettingthepathproperty.Thelocation service to change the URL by setting the path property. The locationservicetochangetheURLbysettingthepathproperty.Thelocation service then updates the browser's address bar with the new URL. In this code, the $locationProvider.html5Mode function is set to false, which means that the $location service will use the hashbang URL syntax instead of the HTML5 pushstate API. This means that the URL will change to #!/Home when the "Go to Home" button is clicked.
HTML `