Improve this Doc View Source $route
- $routeProvider
- service in module ngRoute
Overview
$route is used for deep-linking URLs to controllers and views (HTML partials). It watches $location.url() and tries to map the path to an existing route definition.
Requires the ngRoute module to be installed.
You can define routes through $routeProvider's API.
The $route service is typically used in conjunction with the ngView directive and the $routeParams service.
Dependencies
Methods
-
reload();
-
updateParams(newParams);
Causes
$routeservice to update the current URL, replacing current route parameters with those specified innewParams. Provided property names that match the route's path segment definitions will be interpolated into the location's path, while remaining properties will be treated as query params.Parameters
Param Type Details newParams !Object<string, string>mapping of URL parameter names to values
Events
-
$routeChangeStart
Broadcasted before a route change. At this point the route services starts resolving all of the dependencies needed for the route change to occur. Typically this involves fetching the view template as well as any dependencies defined in
resolveroute property. Once all of the dependencies are resolved$routeChangeSuccessis fired.The route change (and the
$locationchange that triggered it) can be prevented by callingpreventDefaultmethod of the event. See$rootScope.Scopefor more details about event object.Type:
broadcastTarget:
root scopeParameters
Param Type Details angularEvent ObjectSynthetic event object.
next RouteFuture route information.
current RouteCurrent route information.
-
$routeChangeSuccess
Broadcasted after a route change has happened successfully. The
resolvedependencies are now available in thecurrent.localsproperty.ngView listens for the directive to instantiate the controller and render the view.
Type:
broadcastTarget:
root scopeParameters
Param Type Details angularEvent ObjectSynthetic event object.
current RouteCurrent route information.
previous RouteUndefinedPrevious route information, or undefined if current is first route entered.
-
$routeChangeError
Broadcasted if a redirection function fails or any redirection or resolve promises are rejected.
Type:
broadcastTarget:
root scopeParameters
Param Type Details angularEvent ObjectSynthetic event object
current RouteCurrent route information.
previous RoutePrevious route information.
rejection RouteThe thrown error or the rejection reason of the promise. Usually the rejection reason is the error that caused the promise to get rejected.
-
$routeUpdate
Broadcasted if the same instance of a route (including template, controller instance, resolved dependencies, etc.) is being reused. This can happen if either
reloadOnSearchorreloadOnUrlhas been set tofalse.Type:
broadcastTarget:
root scopeParameters
Param Type Details angularEvent ObjectSynthetic event object
current RouteCurrent/previous route information.
Properties
-
current
ObjectReference to the current route definition. The route definition contains:
-
controller: The controller constructor as defined in the route definition. -
locals: A map of locals which is used by $controller service for controller instantiation. Thelocalscontain the resolved values of theresolvemap. Additionally thelocalsalso contain:-
$scope- The current route scope. -
$template- The current route template HTML.
The
localswill be assigned to the route scope's$resolveproperty. You can override the property name, usingresolveAsin the route definition. See $routeProvider for more info. -
-
-
routes
ObjectObject with all route configuration Objects as its properties.
Example
This example shows how changing the URL hash causes the $route to match a route against the URL, and the ngView pulls in the partial.
© 2010–2018 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://code.angularjs.org/1.7.8/docs/api/ngRoute/service/$route