3

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 ?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
gxlcl
  • 31
  • 3

1 Answers1

1

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.

Emin Laletovic
  • 4,084
  • 1
  • 13
  • 22
  • Following worked for me: `servletResponse.sendRedirect("http://xxxxx/yyyy/#!/about/something");` and corresponding code for $stateProvider whereas `servletResponse.sendRedirect("http://xxxxx/yyyy/#!/about?type=something");` gave me type=null, and did not set the value that was passed. – OutOfMind Apr 21 '17 at 08:40
  • 1
    My mistake, that particular case should be matched with `url: '/about?type'`. I will edit this right away. – Emin Laletovic Apr 21 '17 at 09:26