Support us .Net Basics C# SQL ASP.NET Aarvi MVC Slides C# Programs Subscribe Download

AngularJS optional url parameters

Suggested Videos
Part 37 - Difference between $scope and $rootScope
Part 38 - AngularJS cancel route change
Part 39 - AngularJS route change events

In this video we will discuss angular optional URL parameters. Let us understand this with an example. Here is what we want to do.

On the list of students page, we want to search employees by name. 
angular optional route parameter

For example if we type "Ma" and click search button, on the subsequent page we want to display all the student names that start with "Ma" as shown below.
angular route param optional

Here are the steps to achieve this.

Step 1 : Add a method to the web service to retrieve students by name from the database.

public void GetStudentsByName(string name)
    List<Student> listStudents = new List<Student>();

    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
        SqlCommand cmd =
            new SqlCommand("Select * from tblStudents where name like @name", con);
        SqlParameter param = new SqlParameter()
            ParameterName = "@name",
            Value = name + "%"

        SqlDataReader rdr = cmd.ExecuteReader();
        while (rdr.Read())
            Student student = new Student();
   = Convert.ToInt32(rdr["Id"]);
   = rdr["Name"].ToString();
            student.gender = rdr["Gender"].ToString();
   = rdr["City"].ToString();

    JavaScriptSerializer js = new JavaScriptSerializer();

Step 2 : Modify studentsController function in script.js to include studentSearch() function. This function needs to be called when the Search button is clicked.

.controller("studentsController", function ($http, $route, $location) {
    var vm = this;

    vm.studentSearch = function () {
        if (
            $location.url("/studentsSearch/" +

    vm.reloadData = function () {

                .then(function (response) {
                    vm.students =;

Step 3 : Modify students.html partial template in Templates folder as shown below.

<h1>List of Students</h1>

Name : <input type="text" ng-model="" />
<button ng-click="studentsCtrl.studentSearch()">Search</button>

    <li ng-repeat="student in studentsCtrl.students">
        <a href="students/{{}}">

<button ng-click="studentsCtrl.reloadData()">Reload</button>

Step 4 : Add studentsSearchController function in script.js

.controller("studentsSearchController", function ($http, $routeParams) {
    var vm = this;

    if ($ {
            url: "StudentService.asmx/GetStudentsByName",
            method: "get",
            params: { name: $ }
        }).then(function (response) {
            vm.students =;
    else {
                    .then(function (response) {
                        vm.students =;

Step 5 : Include a new route for searching students in script.js. To make a parameter optional, all you have to do is include a "?" at the end.

.when("/studentsSearch/:name?", {
    templateUrl: "Templates/studentsSearch.html",
    controller: "studentsSearchController",
    controllerAs: "studentsSearchCtrl"


Step 6 : Add a new HTML file to the Templates folder. Name it studentsSearch.html. Copy and paste the following HTML.

<h1>Student Search</h1>

<table border="1" style="border-collapse:collapse">
    <tr ng-repeat="student in studentsSearchCtrl.students">

AngularJS tutorial for beginners

No comments:

Post a Comment

It would be great if you can help share these free resources