How can I customize urlerjs ui-router controller parameter from java server redirect?
Defining a route as follows:
.state('about', {
url: '/about',
controller: 'AboutCtrl',
templateUrl: 'views/about/about.html'
})
Java backend:
servletResponse.setHeader("type", "something");
servletResponse.sendRedirect("http://xxxxx/yyyy/#!/about");
So how can I get the "type" parameter in AboutCtrl?
source to share
I don't know much about how to use servletResponse
, but from the method name, setHeader
you are trying to set the header value, not the parameter value.
There are several ways to resolve the parameter using ui-router
(I assume you are using ui-router
). You can check this link for more details on this, but here are several ways:
If you decide to go with servletResponse.sendRedirect("http://xxxxx/yyyy/#!/about/something");
, you can do something like this:
.state('about', {
url: '/about/:type', //or even url: '/about/{type}'
controller: 'AboutCtrl',
templateUrl: 'views/about/about.html'
});
function AboutCtrl($stateParams){
console.log($stateParams.type); //something
}
If you want to use query parameters servletResponse.sendRedirect("http://xxxxx/yyyy/#!/about?type=something");
, you must map your status url to url: '/about?type'
.
If you want to use parameters without defining them in your state url
, you can do something like this:
.state('about', {
url: '/about/',
params: {
type: null
},
controller: 'AboutCtrl',
templateUrl: 'views/about/about.html'
});
function AboutCtrl($stateParams){
console.log($stateParams.type); //something
}
You can find more details and options in the link provided.
source to share