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?