Explain in detail the writing method of obtaining path of parameters through $location in angular

  • 2021-08-05 08:41:32
  • OfStack

I recently in the study of angular through $location access to the path (parameters) writing, in the Internet search a lot of information, just today have time, on the collation of 1!

The following is obtained with the modified URL (http://172.16.0.88: 8100/#/homePage? id=10 & a=100)

'1' Get (without modifying URL)


// Gets the current complete url Path  
var absurl = $locationabsUrl(); 
//http://88:8100/#/homePage?id=10&a=100 
 
//  Gets the current url Path ( Current url# What follows , Including parameters and hash values ): 
 var url = $locationurl(); 
 //  Results: /homePage?id=10&a=100  
 
 //  Gets the current url Sub-path of ( That is, at present url# What follows , Parameters are not included ) 
var pathUrl = $locationpath() 
// Results: /homePage  
 
// Gets the current url The agreement of ( For example http,https) 
var protocol = $locationprotocol(); 
// Results: http  
 
// Get the host name  
var localhost = $locationhost(); 
// Results: 88 
  
// Gets the current url Port of  
var port = $locationport(); 
// Results: 8100 
 
// Gets the current url Hash value of  
var hash = $locationhash() 
// Results: http://088   
 
 // Gets the current url Serialization of parameters of json Object  
 var search = $locationsearch(); 
 // Results: {id: "10", a: "100"} 

"2" Modifications (changes related to URL)


//1  Modify url Sub-path part of ( That is, at present url# What follows , Parameters are not included ): 
 $locationurl('/validation'); 
// Results: http://88:8100/#/validation 
 
 //2  Modify url Hash value part of  
$locationhash('myhash3'); 
// Results :http://88:8100/#/homePage?id=10&a=100#myhash3 
 
//3  Modify url Parameters section of ( No. 1 1 Parameter representation url Attribute name of parameter , No. 1 2 Parameters are the attribute value of the attribute name , If it is an existing property name , Modify the , If it is not an existing attribute , Add ) 
$locationsearch('id','111') 
//  Results ( Modify parameter values ) : http://88:8100/#/homePage?id=111&a=100 
 
$locationsearch('ids','111') 
//  Result (new ids Parameter ): http://88:8100/#/homePage?id=111&a=100&ids=111 
 
//1 Sequential modification of multiple parameters  
$locationsearch({id:'55','a':'66'}) 
// Results: http://88:8100/#/homePage?id=55&a=66#myhash3 
 
 // No. 1 1 Value representation url Attribute name of parameter , If it is an existing property name , Delete the property , If it is not an existing attribute , That means you haven't changed  
 $locationsearch('age',null) 

"3" Modify URL but not save in history

In the above method of modifying url, url will be stored in the history record every time it is modified. You can use the back button to return to url before modification. If you don't want this effect, you can just replace the current record, you can use $location. path ('/validation'). replace ();


Related articles: