Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can angularjs ui-router controller read parameter from a java backend redirect?

Route definition as follow:

.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 parameter of "type" in AboutCtrl ?

like image 801
gxlcl Avatar asked Jul 10 '26 11:07

gxlcl


1 Answers

I don't know much about how to use servletResponse, but from the setHeader method's name, you are trying to set a header value, not a parameter value.

There are couple of ways you can resolve a parameter with ui-router (i'm assuming you are using ui-router). You can check this link for more details on that, but here are some 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 should match your state's URL with url: '/about?type'.

If you want to use parameters without defining them in your state's 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 variations in the provided link.

like image 153
Emin Laletovic Avatar answered Jul 13 '26 20:07

Emin Laletovic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!