When I call the service method from the controller it returns data with delay and I need this response to populate the dropdown
EXPECTATION :
When I call this method from the controller it should return a response immediately then other logic should run after this.
Service Code
app
.service('UserAccounts', ['$http', '$q', function ($http, $q) {
var deferred = $q.defer();
var allInvoices;
this.vendorPromise = $http.get('api/invoices/getAllinvoices', { params: { cache: false } }).then(
function (response){
Invoices = response.data;
deferred.resolve(Invoices);
}).catch(function (err){
});
this.findUserByNameEmail = function (Name, email) {
if (allInvoices) {
return $q.when(allInvoices.filter(function (vendor) {
return user.ReceivingLegalName == Name && user.ReceivingEmail == email ;
}));
}
else {
var findDeferred = $q.defer();
deferred.promise.then(function (Invoices) {
findDeferred.resolve(Invoices.filter(function (user) {
return user.ReceivingLegalName == Name && user.ReceivingEmail == email ;
}));
});
return findDeferred.promise;
}
};
}]);
Controller code :
UserAccounts.findUserByNameEmail($scope.invoice.userName, $scope.invoice.email).then(
function (invoiceDetails) {
$scope.InvoiceList = invoiceDetails;
});
when I try to do it like this it returns a response immediately but gets value inside $$state
$scope.InvoiceList = UserAccounts.findUserByNameEmail($scope.invoice.userName, $scope.invoice.email);
Looks like .then takes some time to return response .could some one help me here