AngularJS optional url parameters

۶۹.۶ K


angular optional route parameter angular route param optional angularjs optional route parameter angularjs route optional parameters angularjs routeparams optional angular optional parameter angular optional params angular optional parameter example in c# angular route params optional AngularJS optional url parameters 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. 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" Here are the steps to achieve this. Step 1 : Add a method to the web service to retrieve students by name from the database. [WebMethod] 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 + "%" }; cmd.Parameters.Add(param); con.Open(); 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(); listStudents.Add(student); } } JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Write(js.Serialize(listStudents)); } 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/" + else $location.url("/studentsSearch") } vm.reloadData = function () { $route.reload(); } $http.get("StudentService.asmx/GetAllStudents") .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] [ul] [li ng-repeat="student in studentsCtrl.students"] [a href="students/{{}}"] {{}} [/a] [/li] [/ul] [button ng-click="studentsCtrl.reloadData()"]Reload[/button] Step 4 : Add studentsSearchController function in script.js .controller("studentsSearchController", function ($http, $routeParams) { var vm = this; if ($ { $http({ url: "StudentService.asmx/GetStudentsByName", method: "get", params: { name: $ } }).then(function (response) { vm.students =; }) } else { $http.get("StudentService.asmx/GetAllStudents") .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"] [thead] [tr] [th]Id[/th] [th]Name[/th] [th]Gender[/th] [th]City[/th] [/tr] [/thead] [tr ng-repeat="student in studentsSearchCtrl.students"] [td]{{}}[/td] [td]{{}}[/td] [td]{{student.gender}}[/td] [td]{{}}[/td] [/tr] [/table] Link for all dot net and sql server video tutorial playlists Link for slides, code samples and text version of the video

Published by: kudvenkat
Published at: ۳ years ago
Category: آموزشی