Location class

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 'package:angular/angular.dart' show Component;
import 'package:angular_router/angular_router.dart'
  show
    Location,
    RouteConfig,
    ROUTER_DIRECTIVES,
    ROUTER_PROVIDERS;

@Component(
  ...
  directives: [ROUTER_DIRECTIVES],
)
@RouteConfig(const [
  ...
])
class AppComponent {
  AppComponent(Location location) {
    location.go('/foo');
  }
}

bootstrap(AppComponent, [ROUTER_PROVIDERS]);
Annotations
  • @Injectable()

Constructors

Location(LocationStrategy locationStrategy)

Properties

locationStrategy LocationStrategy
final
hashCode → int
The hash code for this object. [...]
read-only, inherited
runtimeType → Type
A representation of the runtime type of the object.
read-only, 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
normalizePath(String path) → String
Normalizes path for navigation. [...]
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 if HashLocationStrategy is used, or the appBaseHref if the PathLocationStrategy 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(dynamic value), [ void onThrow(dynamic exception), void onReturn() ]) → Object
Subscribe to the platform's popState events.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a non-existent method or property is accessed. [...]
inherited
toString() → String
Returns a string representation of this object.
inherited

Operators

operator ==(dynamic other) → bool
The equality operator. [...]
inherited

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.