<html xmlns="http://www.w3.org/1999/xhtml" ng-app="serviceEmp"> <head runat="server"> <title></title> <script src="angular.js"></script> <script src="angular.min.js"></script> </head> <body> <form id="form1" runat="server"> <div ng-controller="empController"> search:<input type="text" ng-model="search" /> <table> <tr ng-repeat="emp in employees | filter:search"> <td>{{emp.EmpId }} </td> <td>{{emp.EmpName}} </td> <td>{{emp.Contact }} </td> <td>{{emp.EmailID }} </td> </tr> </table> </div> <script> var app = angular.module('serviceEmp', []); app.controller('empController', function ($scope, $http) { var url = "MY_Service.asmx/HelloWorld"; $http.get(url).success(function (data) { var myjson = JSON.parse(data); $scope.employees = JSON.parse(myjson); }) }); </script> </form> </body> </html>
[WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class MY_Service : System.Web.Services.WebService { RetriveDataByAngularjs retrive = new RetriveDataByAngularjs(); [WebMethod] //[System.Web.Script.Services.ScriptService] [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { JavaScriptSerializer js = new JavaScriptSerializer(); Context.Response.Clear(); Context.Response.ContentType = "application/json"; string str= retrive.ConvertDataTableTo_JSON_String(retrive.GetDataTable()); Context.Response.Write(js.Serialize(str)); return str; } }
public DataTable GetDataTable() { DataTable dataTable = new DataTable(); SqlCommand cmd = new SqlCommand(); cmd.Connection = conn; cmd.CommandText = "select * from Employee"; SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dataTable); return dataTable; } public string ConvertDataTableTo_JSON_String(DataTable dataTable) { System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer(); List<Dictionary<String, Object>> tableRows = new List<Dictionary<String, Object>>(); Dictionary<String, Object> row; foreach (DataRow dr in dataTable.Rows) { row = new Dictionary<String, Object>(); foreach (DataColumn col in dataTable.Columns) { row.Add(col.ColumnName, dr[col]); } tableRows.Add(row); } string str = serializer.Serialize(tableRows); return str; }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)