Basic usage and precautions of $location in AngularJS

1. Configure config
app.config([ '$locationProvider', function($locationProvider) {
$locationProvider.html5Mode({
          //Set to html5Mode(mode), when it is false, it is Hashbang mode
enabled: true,
          //Whether it is required Add the base tag, which is set to false here. When it is set to true, you need to configure the <base href="" /> tag in the html head with
requireBase : false
});
} ]);

Note: the configuration in the config is very important, not configured Can not get the parameters
in the url 2. Basic usage

url: http://127.0.0.1:7001/liuxu/pages/main.html?name=5
1. Get the absolute path
$location.absUrl();  
//url: http://127.0.0.1:7001/liuxu/pages/main.html?name=5
2. Get the host
$location.host(); 
http://127.0.0.1
3. Get the port number
$location.port() ;
//7001
4. Get the text transfer protocol
$location.protocol();
http
5. Get url parameter
$location.search().name or $location.search()['name']

//5
6. Get url
$location.url()
//:/ liuxu/pages/main.html?name=5

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326440772&siteId=291194637