Click here to Skip to main content
15,887,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Consuming web-service through Angular

What I have tried:

I am consuming  a simple web-service from angular. The web-service is returning the appropriate json result. However, the expressions on my angular app is not getting resolved with the json data fed by webservice


Web-Service code:

public class EmployeeService : System.Web.Services.WebService
    {

        [WebMethod]
        public void GetEmployee()
        {
            List<Employee> employeeList = new List<Employee>();

            string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(cs))
            {
                SqlCommand cmd = new SqlCommand("SELECT id, name, gender, annual_salary from tblEmployee", con);
                con.Open();
                SqlDataReader reader = cmd.ExecuteReader();
                while(reader.Read())

                {
                    Employee emp = new Employee();
                    emp.Id = Convert.ToInt32(reader["ID"]);
                    emp.Name = reader["NAME"].ToString();
                    emp.Gender = reader["GENDER"].ToString();
                    emp.Annual_Salary = Convert.ToInt32(reader["Annual_Salary"]);

                    employeeList.Add(emp);
                }
                con.Close();
            }

            JavaScriptSerializer js = new JavaScriptSerializer();
            Context.Response.Write(js.Serialize(employeeList));
        }
    }
}


Angular Controller code:

myModule.controller("ConsumeEmployeeWebService", function ($scope, $http) {
    $http.post('EmployeeService.asmx/GetEmployee')
    .then(function (response) {
        $scope.employees = response.data;
    });
});



HTML :

<!DOCTYPE html>
<html ng-app="myModule">
<head>
    <title>Consuming Employee WebService using Angular</title>
    <script src="Scripts/Script.js"></script>
    <script src="Scripts/angular.min.js"></script>
	<meta charset="utf-8" />
</head>
<body>
    <div ng-controller="ConsumeEmployeeWebService">
        <table>
                <thead>
                    <tr>
                        <th>ID</th>
                        <th>NAME</th>
                        <th>GENDER</th>
                        <th>SALARY</th>
                    </tr>
                </thead>
                <tbody>
                    <tr ng-repeat="employee in employees">
                        <td>{{employee.Id}}</td>
                        <td>{{employee.Name}}</td>
                        <td>{{employee.Gender}}</td>
                        <td>{{employee.Annual_Salary}}</td>
                    </tr>
                </tbody>
        </table>
    </div>
</body>
</html>


WebService Json Result:

[{"Id":1,"Name":"Rohit","Gender":"male","Annual_Salary":50000},{"Id":2,"Name":"Arif","Gender":"male","Annual_Salary":60000},{"Id":3,"Name":"Stefi","Gender":"female","Annual_Salary":70000}]



Final Result:

ID	            NAME	            GENDER	            SALARY
{{employee.ID}}	{{employee.NAME}}	{{employee.GENDER}}	{{employee.Annual_Salary}}
Posted
Updated 17-Apr-17 3:22am
v2

1 solution

Nevermind Guys.... Found the answer after multiple trials. Im so very dumb.

Answer -

Within my HTML,

I swapped the tags -
<script src="Scripts/Script.js"></script>
<script src="Scripts/angular.min.js"></script>


to

<pre><script src="Scripts/angular.min.js"></script>
<script src="Scripts/Script.js"></script>


That is,
Angular library script should be loaded before my custom script file.
 
Share this answer
 
v3
Comments
Bryian Tan 17-Apr-17 9:43am    
What was being swapped again?
Rohcode 17-Apr-17 12:23pm    
Oops!!! I forgot to swap. I have updated the solution now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900