You need to pass two parameters to a state using ui-sref and access them in the state controller. However, you encounter issues with undefined parameter values in the controller.
To pass parameters with ui-sref and retrieve them in the controller, follow these steps:
$stateProvider .state('home', { url: '/:foo?bar', // Define parameter placeholders in the URL views: { '': { templateUrl: 'home.html', controller: 'MainRootCtrl' }, ... } });
.controller('MainRootCtrl', function($scope, $state, $stateParams) { var foo = $stateParams.foo; // Access fooVal var bar = $stateParams.bar; // Access barVal })
In addition to URL parameter placeholders, you can also define granular parameters using the params object in state definitions:
.state('other', { url: '/other/:foo?bar', params: { foo: { value: 'defaultValue', squash: false, }, bar: { array: true, }, hiddenParam: 'YES', } ...
To pass parameters using ui-sref and retrieve them with the updated state definition:
<a ui-sref="home({foo: 'fooVal1', bar: 'barVal1'})"></a> <a ui-sref="home({foo: 'fooVal2', bar: 'barVal2'})"></a>
The params object settings include:
For more information, refer to the ui-router documentation on URL Parameters and $stateProvider.
The above is the detailed content of How to Pass Parameters with ui-sref and Retrieve them in the Controller?. For more information, please follow other related articles on the PHP Chinese website!