0

I have a controller defined as follows with the method getEmployeeDetails:

var app = angular.module('app');
app.controller('AppController',[ '$scope', '$http', function($scope, $http) {

    $scope.result = [];
    $scope.getEmployeeDetails = function() {
        var response = $http.get('http://localhost:8080/empMgt/employee/all');
        response.success(function(data, status, headers, config) {
            $scope.result.push(data);
        });
        response.error(function(data, status, headers, config) {
            alert( "Exception details: " + JSON.stringify({data: data}));
        });
    };

    $scope.getEmployeeDetails();
}]);

This method gets a result object in return that has a list of employees in it. I am trying to show that list in a table in my html.

The html is as follows:

<body ng-controller="AppController" style="display:none">
    {{result}}
    <table>
        <tr>
            <th>EmployeeId</th>
            <th>ProjectId</th>
            <th>Employee Name</th>
            <th>Employee Address</th>
        </tr>
        <tr ng-repeat="employee in result.listOfEntities">
            <td>{{employee.employeeId}}</td>
            <td>{{employee.projectId}}</td>
            <td>{{employee.employeeName}}</td>
            <td>{{employee.employeeAddress}}</td>
        </tr>
    </table>
</body>

The object comes fine in the html as i printed the same. The list is not getting iterated in the table row.

The returned data looks like this:

{
    "result": true,
    "resultCode": "SUCCESS",
    "listOfEntities": [{
        "employeeId": "1",
        "projectId": "1",
        "employeeName": "asdfg",
        "employeeAddress": "asdfg",
        "project": null
    }]
}

Help would be appreciated.

3
  • Could you output what data variable contains ? Are you sure that listOfEntities it's in data object ? Commented Apr 20, 2017 at 13:31
  • Could you post a sample of the objects in result? Commented Apr 20, 2017 at 13:31
  • @PrerakSola I'm trying to iterate over listOfEntities.. Commented Apr 20, 2017 at 13:35

3 Answers 3

2

result.listOfEntities does not exist, beacause result is clearly an array:

$scope.result = [];
$scope.result.push(data);

I imagine you would be better off without the array, and instead:

$scope.result = data;

or even:

$scope.listOfEntities = data.listOfEntities;
ng-repeat="employee in listOfEntities"
Sign up to request clarification or add additional context in comments.

Comments

1

You have declared $scope.result as an array($scope.result=[]). So result.listOfEntites will not resolve to anything. As per your sample data, it should be an object ($scope.result={};) and in the response from your server, you should do $scope.result = data

Comments

0

Why you are use push.. We don't need to push, We can use directly.

var app = angular.module('app',[]);
app.controller('AppController', [ '$scope', '$http', function($scope, $http) {
            $scope.result = [];
$scope.getEmployeeDetails = function() {            
  var data = {
    "result": true,
    "resultCode": "SUCCESS",
    "listOfEntities": [{
        "employeeId": "1",
        "projectId": "1",
        "employeeName": "asdfg",
        "employeeAddress": "asdfg",
        "project": null
    }]
  };
  $scope.result.push(data.listOfEntities[0]);
};
$scope.getEmployeeDetails();
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body ng-app="app" ng-controller="AppController">
    <div class="wrapper row-offcanvas row-offcanvas-left">
        <!-- Main content -->
        <div class="program-section">
            {{result.listOfEntities}}
            <div class="container">
                <table class="table-striped">
                    <tr>
                        <th>EmployeeId</th>
                        <th>ProjectId</th>
                        <th>Employee Name</th>
                        <th>Employee Address</th>
                    </tr>
                    <tr ng-repeat="employee in result">{{employee}}
                        <td>{{employee.employeeId}}</td>
                        <td>{{employee.projectId}}</td>
                        <td>{{employee.employeeName}}</td>
                        <td>{{employee.employeeAddress}}</td>
                    </tr>
                </table>

            </div>
        </div>
        <!-- ./program-section -->
    </div>
    <!-- ./wrapper -->
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.