0

I'm new to angular and I'm creating a simple search app. In my search app i am using factory where i have two function(Search and Demographic that call two different webapi to retrieve data as follow.

MYSearchApp.factory('dataProvider', function($http, $q,$timeout) {



    return {

        search: function (filter) {
            var dataUrl = '/api/xyz/search';
            var deferred = $q.defer();
            $http.post(dataUrl, filter)
                .success(function (data) {
                deferred.resolve(data);
                })
                .error(function (err,status) {
                    if (status == -1) {
                        err = "Server not responding";
                    }
                    deferred.reject(err);
                });

            return deferred.promise;
        },

        Demographics: function (filter) {
            var dataUrl = '/api/xyz/Demographics';
            var deferred = $q.defer();
            $http.post(dataUrl, filter)
                .success(function (data) {

                    deferred.resolve(data);
                })
                .error(function (err,status) {
                    if (status == -1) {
                        err = "Server not responding";
                    }
                    deferred.reject(err);
                });

            return deferred.promise;
        }



    }
});

I am calling the function in my code as below:

MYSearchApp.controller('SearchController',
    function SearchController($scope, $route, $routeParams, dataProvider, consts, $timeout) {

.................
..............


$scope.search = function (searchForm) {


dataProvider.Demographics(filter).then(function(data)   {
                    if (data && data.length > 0) {

                        $scope.Email = data[0].Email;
                    }
                }

                )

dataProvider.search(filter).then(function (data) {

                    if (data && data.length > 0) {

            $scope.Details = data;

        })

        }

}

As of now both the function running at the same time, however i would like to know how i can run this function sequentially i.e. i want to execute demographics first and after the result received i would like to execute search method.

it would be great if someone can share some example how to achieve this. Thanks in advance for the help.

3
  • Check this question it may help. Commented Nov 8, 2016 at 16:16
  • Are you using AngularJS 1.x (angularjs) or Angular 2 (angular2)? Commented Nov 8, 2016 at 16:21
  • I am using AngularJS 1.x Commented Nov 9, 2016 at 6:47

0

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.