Location
is a service that applications can use to interact with a browser's URL.
Depending on which LocationStrategy is used, Location
will either persist
to the URL's path or the URL's hash segment.
Note: it's better to use Router#navigate
service to trigger route changes. Use
Location
only if you need to interact with or create normalized URLs outside of
routing.
Location
is responsible for normalizing the URL against the application's base href.
A normalized URL is absolute from the URL host, includes the application's base href, and has no
trailing slash:
- /my/app/user/123
is normalized
- my/app/user/123
is not normalized
- /my/app/user/123/
is not normalized
Example
import {Component} from 'angular2/core';
import {Location} from 'angular2/platform/common';
import {
ROUTER_DIRECTIVES,
ROUTER_PROVIDERS,
RouteConfig
} from 'angular2/router';
@Component({directives: [ROUTER_DIRECTIVES]})
@RouteConfig([
{...},
])
class AppCmp {
constructor(location: Location) {
location.go('/foo');
}
}
bootstrap(AppCmp, [ROUTER_PROVIDERS]);
- Annotations
Static Methods
-
joinWithSlash(
String start, String end) → String -
Given 2 parts of a url, join them with a slash if needed.
-
normalizeQueryParams(
String params) → String -
Given a string of url parameters, prepend with '?' if needed, otherwise return parameters as is.
-
stripTrailingSlash(
String url) → String -
If url has a trailing slash, remove it, otherwise return url as is.
Constructors
- Location(LocationStrategy platformStrategy)
Properties
- hashCode → int
-
Get a hash code for this object.
read-only, inherited - platformStrategy → LocationStrategy
-
read / write
- runtimeType → Type
-
A representation of the runtime type of the object.
read-only, inherited
Operators
-
operator ==(
other) → bool -
The equality operator.
inherited
Methods
-
back(
) → void -
Navigates back in the platform's history.
-
forward(
) → void -
Navigates forward in the platform's history.
-
go(
String path, [ String query = "" ]) → void -
Changes the browsers URL to the normalized version of the given URL, and pushes a new item onto the platform's history.
-
hash(
) → String -
normalize(
String url) → String -
Given a string representing a URL, returns the normalized URL path without leading or trailing slashes
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a non-existent method or property is accessed.
inherited -
path(
) → String -
Returns the normalized URL path.
-
prepareExternalUrl(
String url) → String -
Given a string representing a URL, returns the platform-specific external URL path. If the given URL doesn't begin with a leading slash (
'/'
), this method adds one before normalizing. This method will also add a hash ifHashLocationStrategy
is used, or theAPP_BASE_HREF
if thePathLocationStrategy
is in use. -
replaceState(
String path, [ String query = "" ]) → void -
Changes the browsers URL to the normalized version of the given URL, and replaces the top item on the platform's history stack.
-
subscribe(
void onNext(value), [ void onThrow(exception) = null, void onReturn() = null ]) → Object -
Subscribe to the platform's
popState
events. -
toString(
) → String -
Returns a string representation of this object.
inherited