Angular gets and modifies the current page URL by injecting $location

Source: Internet
Author: User

Tag:java   for    Hash    comment   hash    comm   form    using    mil   

below to get and modify the URL to   ( http://172.16.0.88:8100/#/homePage?id=10&a=100   )   For example

"One" gets (does not modify the URL)

[JavaScript]View PlainCopy
  1. 1. Get the current full URL path
  2. var absurl = $location. Absurl ();
  3. http://172.16.0.88:8100/#/homePage?id=10&a=100
  4. 2. Gets the current URL path (the contents after the current url#, including parameters and hashes):
  5. var url = $location. URL ();
  6. //Result:/homepage?id=10&a=100
  7. //3. Gets the sub-path of the current URL (that is, the content after the current url#, excluding parameters)
  8. var pathurl = $location. Path ()
  9. Results:/homepage
  10. 4. Get the protocol for the current URL (e.g. Http,https)
  11. var protocol = $location. Protocol ();
  12. Result: http
  13. 5. Get Host Name
  14. var localhost = $location. Host ();
  15. Results: 172.16.0.88
  16. 6. Get the port of the current URL
  17. var port = $location. Port ();
  18. Results: 8100
  19. 7. Get the hash value of the current URL
  20. var hash = $location. Hash ()
  21. Results: http://172.16.088
  22. //8. To get the serialized JSON object for the parameters of the current URL
  23. var search = $location. Search ();
  24. //Result: {ID: "Ten", A: " +"}

"Two" Changes (URL related content)

[JavaScript]View PlainCopy
  1. 1 Modify the sub-path portion of the URL (that is, the contents after the current url#, excluding parameters):
  2. $location. URL ('/validation ');
  3. Results: Http://172.16.0.88:8100/#/validation
  4. //2 Modifying the hash value portion of a URL
  5. $location. Hash (' myhash3 ');
  6. Results: HTTP://172.16.0.88:8100/#/HOMEPAGE?ID=10&A=100#MYHASH3
  7. 3 Modify the parameter portion of the URL (the first parameter represents the property name of the URL parameter, the second parameter is the property value of the property name, and if it is an existing attribute name, the modification, if not an existing attribute, is added)
  8. $location. Search (' id ',' 111 ')
  9. Result (Modify parameter value): http://172.16.0.88:8100/#/homePage?id=111&a=100
  10. $location. Search (' IDs ',' 111 ')
  11. Result (new IDs parameter): http://172.16.0.88:8100/#/homePage?id=111&a=100&ids=111
  12. 4. Modify multiple parameters at once
  13. $location. Search ({id: ' + ',' a ':' "}")
  14. Results: HTTP://172.16.0.88:8100/#/HOMEPAGE?ID=55&A=66#MYHASH3
  15. //5. The first value represents the property name of the URL parameter, and if it is an existing property name, it is deleted if it is not an existing attribute.
  16. $location. Search (' age ',null)

 

" three "modify URL but do not deposit history
            

In the above method of modifying the URL, each time the URL will be stored in the history, you can use the Back button to return to the pre-modified URL, if you do not want this effect, but simply replace the current record, you can use the $location. Path ('/validation '). Replace ();



Angular gets and modifies the current page URL by injecting $location

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.