I have a customer search view that, by default, simply loads a form for first and last name. It can, however, take those params as arguments in the URL. My app config contains:
$stateProvider .state({ name: "search", url: "/search", templateUrl: "partials/customerSearch.html", controller: "CustomerSearchCtrl" }) .state({ name: "searchGiven", url: "/search/:fn/:ln", templateUrl: "partials/customerSearch.html", controller: "CustomerSearchCtrl" })
This works, but it seems like it has unnecessary redundancies. Is there a better way? Is this something $urlRouterProvider
should handle?
What Is the $stateParams Service in Angular? $stateParams is a service that captures URL-based parameters, and we can use these parameters to display information according to the state. Let's create an example with two states, understand how states work, and use $stateparams to store parameters.
Angular-UI-Router is an AngularJS module used to create routes for AngularJS applications. Routes are an important part of Single-Page-Applications (SPAs) as well as regular applications and Angular-UI-Router provides easy creation and usage of routes in AngularJS.
ui-sref-active can live on the same element as ui-sref / ui-state , or it can be on a parent element. If a ui-sref-active is a parent to more than one ui-sref / ui-state , it will apply the CSS class when any of the links are active.
Parameters provide a way of passing arbitrary data to a page via the URL. Optional parameters allow URLs to matched to routes even if no parameter value is passed. Things can get a bit complicated if you want to permit multiple optional parameters.
There's an issue in ui-router tracker about optional parameters. As of now, you can not specify them in clear way, but you can use regular expressions:
url: '/search{fn:(?:/[^/]+)?}'
or query parameters:
url: '/search?fn&ln'
People are working on it, though, so I'd expect desired functionality to land sometime in the future.
You can use:
$stateProvider .state({ name: "searchGiven", url: "/search/{fn}/{ln}", params: { fn: {value: 'foo'}, ln: {value: 'bar'} }, templateUrl: "partials/customerSearch.html", controller: "CustomerSearchCtrl" })
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With