RouteParams In AngularJS

Overview

In this article, we will see how to pass and retrieve parameters from URL in AngularJS. We will be using the same example which we had used in AngularJS routing i.e our Demo Application. In that, we are displaying the list of students with the help of web service and students controller in the URL and after displaying the data from AngularJS Controller, we will add a hyperlink to each of the student record and display various details.

result

For more articles on AngularJS, refer to

Introduction

When we run the application, we see the local route i.e our localhost and students.

application

At the moment, when we click on students' links, we see a local route as student and we are displaying list of students. Here, we will display a hyperlink on each of the students and display various details of each student.

To achieve this, we are going to add a web method to this, which is going to fetch the student details for each student. We will add that webmethod to our webservice.

So, we will create another webservice method called GetStudents and pass a parameter as id.  And, we will pass that parameterized id in our SQL where condition as we are displaying records on the basis of where id=1 and so on. So, our code is for GetStudents web method is:
  1. [WebMethod]  
  2. public void GetStudents(int id )  
  3. {  
  4.     Student student = new Student();  
  5.     string cs = ConfigurationManager.ConnectionStrings["Test"].ConnectionString;  
  6.     using (SqlConnection con = new SqlConnection(cs))  
  7.     {  
  8.         SqlCommand cmd = new SqlCommand("select * from students where id=@id", con);  
  9.   
  10.         SqlParameter param = new SqlParameter()  
  11.         {  
  12.              ParameterName = "@id",  
  13.             Value=id  
  14.         };  
  15.         cmd.Parameters.Add(param);  
  16.         con.Open();  
  17.         SqlDataReader sdr = cmd.ExecuteReader();  
  18.         while (sdr.Read())  
  19.         {  
  20.            
  21.             student.id = Convert.ToInt32(sdr["id"]);  
  22.             student.name = sdr["Name"].ToString();  
  23.             student.gender = sdr["gender"].ToString();  
  24.             student.city = sdr["city"].ToString();  
  25.            
  26.   
  27.   
  28.         }  
  29.   
  30.     }  
  31.   
  32.     JavaScriptSerializer js = new JavaScriptSerializer();  
  33.     Context.Response.Write(js.Serialize(student));  
  34. }  
As you can notice that we have passed a parameterName as @id and value as id with its respective id mapped to each particular student. We are passing that in JSON serialization context to display them.

Let's quickly run our webservice and see our code. In that, we will see two methods. We will invoke the second one as,

methods

Let's click on the second one, GetStudents. We will invoke that.

GetStudents

Now, it's asking for id. We will enter id one and see that we are getting any JSON output. As we have entered one, we got the desired output.

output
Now, we will add hyperlinks to our students controller. Let's switch back to our students controller and add hyperlinks to each of the students as,

Just add an anchor tag.
  1. <a href="students/{{student.id}}">  
Close the anchor Tag, save the changes, and reload the page. You will see a hyperlink on each of the students.

application

Now, just right click on any name and copy the address. Paste that in a notepad and you will see a id appeared as
http://localhost:59601/students/1

Here, in this example, I clicked on Akshay where his id is 1.

The next step is to create a controller in our script file.
  1. .when("/students/:id", {  
  2. templateUrl: "Templates/StudentDetail.html",  
  3. controller: "StudentDetailController"  
  4. })  
We have written students/:id as it is identified by id and controller name as studentdetailcontroller. We need to create a separate page in templates folder to display details page.

So, let's create a page for StudentDetail.

We need to display id,city, and gender of each student. As it’s a partial template, we don’t need the html tags to remove that. Let's bind those records,
  1. <h1>Student Details</h1>  
  2. <table style="border:1px solid black">  
  3.     <tr>  
  4.         <td>Id</td>  
  5.         <td>{{student.id}}</td>  
  6.     </tr>  
  7.     <tr>  
  8.         <td>Name</td>  
  9.         <td>{{student.name}}</td>  
  10.     </tr>  
  11.     <tr>  
  12.         <td>City</td>  
  13.         <td>{{student.city}}</td>  
  14.   
  15.     </tr>  
  16.     <tr>  
  17.         <td>Gender</td>  
  18.         <td>{{student.gender}}</td>  
  19.     </tr>  
  20. </table>  
  21. <a href="students.html">Back To List </a>  
You notice that we are binding those records and you notice we have just written a student object as where these object is coming from. We are doing this in partial template, we will be attaching those in $scope object . Now, we have our partial template ready. The next step is to create a student controller in our script.js file. So, let's create a controller.
  1. .controller("StudentDetailController"function ($scope, $http, $routeParams) {  
  2.     $http({  
  3.         url: "StudentService.asmx/GetStudents",  
  4.         params: { id: $routeParams.id },  
  5.         method:"get"  
  6.     })  
  7.     .then(function (response) {  
  8.   
  9.         $scope.student = response.data;  
  10.   
  11.     })  
  12.   
  13. });  
Now, notice that we have written the controller name as studentdetailcontroller and we are injecting $routeparams the URL we had specified which we are fetching from web service method GetStudents. We are specifying the id as we are displaying records on id and here, we had specified $routeparams.id as method is get.

Just save the changes and reload the page. You will see the List of the entries.

When you click on the particular student, notice the URL in the Output as it appends to 1,2 and so on.

demo

Conclusion

So, this was about RouteParams in AngularJS. Hope this article was helpful.

Up Next
    Ebook Download
    View all
    Learn
    View all