1

I am creating a web application for my school. Angularjs is very new to me and I only begun to use it last week Saturday.

I am trying to create an advanced search form with optional fields and only one compulsory field. When the user submits, the form, I'll redirect the user to a results page which will have the form values as parameters, and that page will provide the result of the search via ajax. I understand that in Angularjs routing, we can set optional parameters, which is really good.

According to my understanding, getting the url parameters is based on the position of the parameter.

Eg. Below is my routing code:

 .when("/results/:school_year/:name?/:age?/:quote?", {templateUrl: "pages/results.html", controller: "SearchCtrl"})

As you guys can see, :school_year is the only compulsory parameter. The remaining are optional. This means that if my url is http://example.com/#/results/2012/Kyle/18/Test

$routeParams.school_year = 2012
$routeParams.name = "Kyle"

Here is the problem, my form has optional fields so this means that it is likely :name or any other optional parameter will have no data when submitted. One option I figured out was to add a double slash (//) to the url where the parameter has no data. I do this by checking whether the optional parameters are null, or they have data. If they are null, I add // to the url, if they are not, then I add a / and the parameter. This is to dynamically build the redirection url. eg.

http://example.com/#/results/2012//18/Test

This involves lots of comparison and it's not clean. Is there any cleaner trick or method to achieve this?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
kofi1995
  • 56
  • 1
  • 6
  • 3
    Kinda feel like query parameters might be more suitable for this case – Kevin B Oct 15 '15 at 22:25
  • If so, how do I get the search form to pass it's values to the url as query parameters? – kofi1995 Oct 15 '15 at 22:34
  • @kofispaceman check out http://stackoverflow.com/questions/13760070/angularjs-passing-data-to-http-get-request – Kevin Friedheim Oct 16 '15 at 05:38
  • I've seen that question @KevinFriedheim. It doesn't solve my problem – kofi1995 Oct 16 '15 at 18:41
  • @kofispaceman are you saying that using query parameters won't solve your problem? It would be trivial to do it seems like... or maybe I'm misunderstanding the issue. – Kevin Friedheim Oct 16 '15 at 20:47
  • @KevinFriedheim I believe I've solved it using query parameters. I didn't know how to set query parameters using angularjs but I now know. When I submit the form, I change the view to another page and pass the form parameters to the new page using $location.search( ); Thanks for your input guys. This works perfectly. – kofi1995 Oct 16 '15 at 21:23

0 Answers0