0

How do I fetch table from db with promise. I have created a service and http call with promise. I have checked the console and the url is not getting called. I am not sure this is the eight way of creating service return a http promise

<!doctype html>
<html lang="en" ng-app="myApp">
    <head>
        <meta charset="utf-8">
        <base href="/">
        <title>The Single Page Blogger</title>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.7/angular-resource.min.js"></script>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
        <script data-require="ui-bootstrap@*" data-semver="0.12.1" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.1.min.js"></script>
        <script src="<%=request.getContextPath()%>/js/module.js"></script>
        <link rel="stylesheet" href="<%=request.getContextPath()%>/style2.css" />
        <script>

            app.controller('tableController', function ($log, $scope, tableService)
            {
                $scope.customerTable = [];
                var promise = tableService.fetchTable();
                promise.then(function (data)
                {
                    console.log("Your name is: " + data);
                    $scope.customerTable = data;
                });
            });

            app.factory('tableService', function ($http)
            {
                return
                {
                    fetchTable: function()
                    {
                        return $http.get('<%=request.getContextPath()%>/GetTable.do');
                    }
                }
            });

        </script>
    </head>
    <body>
        <div class="container" id="main"><br/><br/>
            Search: <input type="text" ng-model="search" placeholder="Search">
            <div ng-controller="tableController">
                <table class="table table-striped table-hover table-bordered">
                    <tr>
                        <th style="font-size: 13.3px">Card number</th>
                        <th style="font-size: 13.3px">First name</th>
                        <th style="font-size: 13.3px">Opening balance</th>
                        <th style="font-size: 13.3px">Withdrawal</th>
                        <th style="font-size: 13.3px">Deposit</th>
                        <th style="font-size: 13.3px">Closing balance</th>
                        <th style="font-size: 13.3px">Tx date</th>
                        <th style="font-size: 13.3px">Usage type</th>
                    </tr>
                    <tr ng-repeat="data in filteredTodos| filter: search">
                        <td>{{data.CARD_NUMBER}}</td>
                        <td>{{data.FIRST_NAME}}</td>
                        <td>{{data.OPENING_BALANCE}}</td>
                        <td>{{data.WITHDRAWAL}}</td>
                        <td>{{data.DEPOSIT}}</td>
                        <td>{{data.CLOSING_BAL}}</td>
                        <td>{{data.TXDATE}}</td>
                        <td>{{data.USAGE_TYPE}}</td>
                    </tr>
                </table>
                <pagination 
                    ng-model="currentPage"
                    total-items="customerTable.length"
                    max-size="maxSize"  
                    boundary-links="true">
                </pagination>
                <br/><br/><br>
                <button class="form-control btn btn-success" type="submit" ng-click="fetchTable()">Load Table Data</button>
            </div>
        </div>
    </body>
</html>

2 Answers 2

1

Your service makes the promise but does not deliver, try adding the $q service into yours like this:

app.factory('tableService', function ($http, $q) {
    return {
        fetchTable: function() {
            // the $http API is based on the deferred/promise APIs exposed by the $q service
            // so it returns a promise for us by default
            return $http.get('<%=request.getContextPath()%>/GetTable.do')
                .then(function(response) {
                    if (typeof response.data === 'object') {
                        return response.data;
                    } else {
                        // invalid response
                        return $q.reject(response.data);
                    }
                }, function(response) {
                    // something went wrong
                    return $q.reject(response.data);
                });
        }
    };
});

app.controller('tableController', function ($log, $scope, tableService)
{
    $scope.customerTable = [];

    $scope.getData = function() {
        var promise = tableService.fetchTable();

        promise.then(function (data)
        {
            console.log("Your name is: " + data);

            $scope.customerTable = data;
        });
    }
});

<div ng-controller="tableController" ng-init="getData()">
Sign up to request clarification or add additional context in comments.

11 Comments

There's no controller? How do I call it in html? just call the fetchTable() with button click?
Tried this: <button type="submit" ng-click="fetchTable()">Load Table Data</button> still not working
Your controller remains the same, although I would add your service call intro a method and call ng-init on the div
You do not need the $q service when returning a promise like he does. Only if you want to create a deferred promise manually and hooking a resolve and reject to it.
Should work though, especially with the init so his code does not execute on controller creation
|
1
app.controller('tableController', function ($log, $scope, tableService)
    {
        $scope.customerTable = [];
        var promise = tableService.fetchTable();
        promise.then(function (data)
        {
            console.log("Your name is: " + data);

            $scope.customerTable = data;
        });
    });

This should do it. You were returning the data before setting the $scope.customerTable. As a matter of fact, why are you even returning data? You can't return something from a callback like that. I've removed that line also.

4 Comments

http url is not getting called. I changed my code as you have said. Did I write the service correctly?
@kittu Can you put a console log before you return the $http.get? just to make sure that the function itself is running correctly. Try adding a .catch after your then() also and log the error to see if it says anything.
Like this: catch (function(error) { console.log("An error occured: " + error); ??
Did the fetchTable function run? Did you try that?

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.