$urlMatcherFactory
ui.router.util
Factory for UrlMatcher instances. The factory
is also available to providers under the name $urlMatcherFactoryProvider.
Creates a UrlMatcher for the specified pattern.
| Param | Type | Details |
|---|---|---|
| pattern | string | The URL pattern. |
| config | Object | The config object hash. |
| UrlMatcher | The UrlMatcher. |
Sets the default behavior when generating or matching URLs with default parameter values.
| Param | Type | Details |
|---|---|---|
| value | string | A string that defines the default parameter URL squashing behavior.
|
Registers a custom Type object that can be used to
generate URLs with typed parameters.
| Param | Type | Details |
|---|---|---|
| name | string | The type name. |
| definition | ObjectFunction | The type definition. See
|
| definitionFn | ObjectFunction | (optional) A function that is injected before the app
runtime starts. The result of this function is merged into the existing |
| Object | Returns |
This is a simple example of a custom type that encodes and decodes items from an array, using the array index as the URL-encoded value:
var list = ['John', 'Paul', 'George', 'Ringo'];
$urlMatcherFactoryProvider.type('listItem', {
encode: function(item) {
// Represent the list item in the URL using its corresponding index
return list.indexOf(item);
},
decode: function(item) {
// Look up the list item by index
return list[parseInt(item, 10)];
},
is: function(item) {
// Ensure the item is valid by checking to see that it appears
// in the list
return list.indexOf(item) > -1;
}
});
$stateProvider.state('list', {
url: "/list/{item:listItem}",
controller: function($scope, $stateParams) {
console.log($stateParams.item);
}
});
// ...
// Changes URL to '/list/3', logs "Ringo" to the console
$state.go('list', { item: "Ringo" });
This is a more complex example of a type that relies on dependency injection to interact with services, and uses the parameter name from the URL to infer how to handle encoding and decoding parameter values:
// Defines a custom type that gets a value from a service,
// where each service gets different types of values from
// a backend API:
$urlMatcherFactoryProvider.type('dbObject', {}, function(Users, Posts) {
// Matches up services to URL parameter names
var services = {
user: Users,
post: Posts
};
return {
encode: function(object) {
// Represent the object in the URL using its unique ID
return object.id;
},
decode: function(value, key) {
// Look up the object by ID, using the parameter
// name (key) to call the correct service
return services[key].findById(value);
},
is: function(object, key) {
// Check that object is a valid dbObject
return angular.isObject(object) && object.id && services[key];
}
equals: function(a, b) {
// Check the equality of decoded objects by comparing
// their unique IDs
return a.id === b.id;
}
};
});
// In a config() block, you can then attach URLs with
// type-annotated parameters:
$stateProvider.state('users', {
url: "/users",
// ...
}).state('users.item', {
url: "/{user:dbObject}",
controller: function($scope, $stateParams) {
// $stateParams.user will now be an object returned from
// the Users service
},
// ...
});