Reputation: 7225
Is it possible to pass an optional param into a resolve function for a configured state?
So something like
resolve: {
resA: getSomething(1),
resB: getSomethingElse
}
function getSomething(num) {
if(num===1) {
//do something here
}
return something
}
function getSomethingElse() {
)
I am looking to reuse the resolve function on other states, but in certain cases I require different logic to be executed inside the function.
Can anyone offer some suggestions?
Thanks
EDIT
OK, so I am actually looking at passing in a custom param to a resolve function that is dependant on an earlier resolve function.
resolve: {
resA: getSomething,
resB: getSomethingElse(1)
}
function getSomething() {
return something
}
function getSomethingElse(num, resA) {
)
In this example, the second resolve gets fired immediately as soon as the state is hit and doesn't wait until resA
gets resolve, so I can console.log out the num
param but resA
is undefined at that point.
Upvotes: 0
Views: 130
Reputation: 691685
Let's take an example:
resolve: {
resA: getSomething,
resB: getSomethingElse(1)
}
the values of resA and resB must be functions. It's the case for resA. For resB, it's the case only if getSomethingElse(1)
returns a function. So getSomethingElse must look like
function getSomethingElse(value) {
return function() {
// ...
}
}
Let's suppose that getSomething
returns 'hello'. And let's suppose you want the function returned by getSomethingElse(1)
to use that value hello
. You thus need to tell ui-router that the function referenced by resB
needs the result of resA
as argument:
function getSomethingElse(value) {
return function(resA) { // the name of the argument MUST BE resA. This function will be injected
// ...
}
}
And now you can thus use both value and resA in that function. For example, to do an http call depending on the value and on resA:
function getSomethingElse(value) {
return function($http, resA) {
if (value === 0) {
return $http.get('/zero').then(...);
}
else {
return $http.get('/one/' + resA).then(...);
}
}
}
Upvotes: 1