©
This document uses PHP Chinese website manual Release
The $location service parses the URL in the browser address bar (based on the window.location) and makes the URL available to your application. Changes to the URL in the address bar are reflected into $location service and changes to $location are reflected into the browser address bar.
The $location service:
For more information see Developer Guide: Using $location
$rootElement
absUrl();
This method is getter only.
Return full url representation with all segments encoded according to rules specified in RFC 3986.
string |
full url |
url([url], [replace]);
This method is getter / setter.
Return url (如: /path?a=b#hash
) when called without any parameter.
Change path, search and hash, when called with parameter and return $location
.
参数 | 类型 | 详述 |
---|---|---|
url
(可选)
|
string |
New url without base prefix (如: |
replace
(可选)
|
string |
The path that will be changed |
string |
url |
protocol();
This method is getter only.
Return protocol of current url.
string |
protocol of current url |
host();
This method is getter only.
Return host of current url.
string |
host of current url. |
port();
This method is getter only.
Return port of current url.
Number |
port |
path([path]);
This method is getter / setter.
Return path of current url when called without any parameter.
Change path when called with parameter and return $location
.
注意: Path should always begin with forward slash (/), this method will add the forward slash if it is missing.
参数 | 类型 | 详述 |
---|---|---|
path
(可选)
|
string |
New path |
string |
path |
search(search, [paramValue]);
This method is getter / setter.
Return search part (as object) of current url when called without any parameter.
Change search part when called with parameter and return $location
.
// given url http://example.com/#/some/path?foo=bar&baz=xoxo
var searchObject = $location.search();
// => {foo: 'bar', baz: 'xoxo'}
// set foo to 'yipee'
$location.search('foo', 'yipee');
// => $location
参数 | 类型 | 详述 |
---|---|---|
search | stringObject.<string>Object.<Array.<string>> |
New search params - string or hash object. When called with a single argument the method acts as a setter, setting the If the argument is a hash object containing an array of values, these values will be encoded as duplicate search parameters in the url. |
paramValue
(可选)
|
stringArray.<string>boolean |
If If If If |
Object |
If called with no arguments returns the parsed |
hash([hash]);
This method is getter / setter.
Return hash fragment when called without any parameter.
Change hash fragment when called with parameter and return $location
.
参数 | 类型 | 详述 |
---|---|---|
hash
(可选)
|
string |
New hash fragment |
string |
hash |
replace();
If called, all changes to $location during current $digest
will be replacing current history
record, instead of adding new one.
Broadcasted before a URL will change. This change can be prevented by calling
preventDefault
method of the event. See $rootScope.Scope
for more
details about event object. Upon successful change
$locationChangeSuccess is fired.
Broadcasted after a URL was changed.