Reputation: 12545
I have an AngularJS app that uses ui-router to manage my app's states and URL routing.
Here is a sample state:
.state('my_state', {
url: '/my_state/:arg1',
templateUrl: 'http://www.example.com/html/my_file.html',
controller: 'MyCtrl'
})
Here is it's controller:
app.controller('MyCtrl', function($stateParams) {
console.log('arg1 = ', $stateParams.arg1)
);
Here is how I send people to it from within another controller:
$state.go('my_state', {'arg1': 'hello'});
When the $state.go
line above is executed, I get sent to this URL /my_state/hello
and I see the following printed to the browser's debug window:
arg1 = hello
Now here is my question: Can I add another argument to this controller such that:
arg2
.MyCtrl
.If so, please show me how.
Upvotes: 1
Views: 2505
Reputation: 4331
Using Parameters without Specifying Them in State URLs
You still can specify what parameters to receive even though the parameters don't appear in the url. You need to add a new field params in the state and create links as specified in Using Parameters in Links
For example, you have the state.
.state('contacts', {
url: "/contacts",
params: {
param1: null
},
templateUrl: 'contacts.html'
})
The link you create is
<a ui-sref="contacts({param1: value1})">View Contacts</a>
Or you can pass them to $state.go()
too.
$state.go('contacts', {param1: value1})
Upvotes: 0
Reputation: 18954
Route parameters in UI Router are optional by default. For example you code (following) says that arg1
is an optional parameter. So both /my_state/:arg1
and /my_state/
will work, but not /my_state
(no trailing slash). It also reveals the arg1
value in the URL.
.state('my_state', {
url: '/my_state/:arg1',
templateUrl: 'http://www.example.com/html/my_file.html',
controller: 'MyCtrl'
})
You Can add another optional argument to this controller with your preferences :
.state('my_state', {
url: '/my_state/:arg1',
templateUrl: 'http://www.example.com/html/my_file.html',
controller: 'MyCtrl',
params: {
arg2: { value: null }
}
})
To hide parameters you have to define them in params
.
Note that the squash
property (inside params
) configures how a default parameter value is represented in the URL when the current parameter value is the same as the default value. If squash is not set, it uses the configured default squash policy.
Update
Another Working Plunker which uses $state.go('my_state', {'arg1': 'hello', 'arg2': 'world'});
inside controller.
Note that there is no functional difference between ui-sref
and $state.go
for Activating a state
Upvotes: 1
Reputation: 6728
Try this:
.state('my_state', {
url: '/my_state',
templateUrl: 'http://www.example.com/html/my_file.html',
controller: 'MyCtrl',
params: null
})
Upvotes: 0
Reputation: 181
.state('someState', {
url: '/my_state?arg1',
templateUrl: 'http://www.example.com/html/my_file.html', // I assume this is correct for the way you set it up.
controller: 'MyCtrl',
params: { // This is the part you'll add
arg2: {
value: null, // you can set a default value
squash: true
}
}
})
This should do the trick for you! I've used it plenty of times! Very useful.
Upvotes: 1