Tutorial 10: Angularjs routeParams Example | Angularjs $routeParams

Angularjs routeParams is a service that allows you to retrieve the current set of route parameters(URL Parameters). In previous tutorial, we had the basic structure so that our application can handle routing but we were only using a single partial. All it does is show a list of guitars. It would be great to make list elements click through more detailed view of the guitar information i.e jump to another page that holds detail of clicked guitar. We can do this by wiring up a second partial using $routeParams service.

If you are following along previous tutorial you will have project files otherwise you can simply download last tutorial files here


In app.js document we gonna put second parameter for second partial because we are going to make multiple files linked. code like this:

Code Explanation:
Its the same code we added in previous tutorial which tells that when somebody goes to /details and asks for a :guitarID (will get to it later) then goto template URL partials/details.html in project folder and get details.html file(get to it later). Finally, DetailsController is controller handling details.html file. Easy enough.


Lets make DetailsController in controllers.js file which will handle detail.html page like this:

Code Explanation:

What I did is to copy the existing controller and paste it right below it and then change the new controller name to DetailsController (it is matching with controller name specified in app.js)and use $routeParams which is responsible for route tracking, you can say it as route manager. We use $routeParams because we need guitarID in our URL as well with routed page of details.html. Here is how link looks like when clicked on guitar: 



We need to make clickable links on guitar from which user can navigate to details.html page. To do this, goto list.html (this is where our guitar list is) and add the following code just after li to enclose the whole guitar in anchor tag like this:

Code Explanation:

What we are going to pass here? I mean in the link? answer is simple, we are going to details.html page with item id on which we clicked. #/details tells that you need to JUMP to details section which is handled by angular in app.js file. Next guitarVariable is the variable you will find in controllers.js file which holds the data from data.json file. Next indexOf will get the position of guitar coming from data.json file and tells that for example violin is at fourth place in data.json file so it will return 3 because its an array, it starts from zero, remember that? I am sure you do. item is an item in guitarVariable. Okay and then after guitar details we closed the anchor tag.

You may be wondering how it gonna show us different id for different guitar? this is because above anchor tag we are using ng-repeat that iterates through data.json file.


Ok now we need to create our details page i.e details.html file in partials folder because we gave its path in app.js file. Lets copy the following code to details.html file and place this file in partials folder of your project:

Code explanation:

We know that guitarVariable is the variable in controllers.js file holding data coming from data.json file. So we created a model named as guitarVariable.  Then we used guitarVariable and asked for whichGuitar, whichGuitar is the itemID or guitarID coming from list.html file along with details.html file like this sitename.com/index.html#/details/2 so we in other words we are specifiing the details of that guitar whose id is coming in whichGuitar. Remember that whichGuitar is already defined in controllers.js file. Rest of the code is just about placing guitar detail(name, dateAdded, price, longDescription etc) in some good way.  Finally we need to add some styling in style.css, add some data in data.json file and an image. You can download these three files from here Download (it has 3 files.

1. Place style.css in root directory of your project and replace existing style.css file

2. Place data.json file inside js folder and replace the existing file

3. put violin.jpg file inside img folder)

All done. Now open index.html file in FIREFOX browser. You will see guitar list and when you click on a guitar, its detail will show up like this:

and link looks like this:

Download complete project

Important Update

David Raasch figured out issue and solution in this tutorial:

Problem:  Hmmm… IE8 doesn’t seem to want to click through to the Details. I’ve confirmed I’m in “Browser Mode: IE8”, “Document Mode: IE8 Standards”. (If I switch to “Quirks Mode”, then everything other than the “Developer’s Blog” banner vanishes.) I’ll keep playing with it.

Solution: Figured it out. IE 8 and lower do not know what .indexOf is. I found the fix here: http://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8  Applied it to your demo and now it’s pulling in the guitarID’s just fine.

Thanks for help David Raasch.

Hope you like it. Please share my work.

next-AngularJS MVCPrevious-Chapter-Button