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.
angularjs) or Angular 2 (angular2)?