I dont want to do the same http call accross controllers so i want to make a service wich fetches the data and returns data to the controller i tried to do this
the controller:
App.controller('SearchCtrl', function ($scope, $rootScope, $http, $location, ngDialog, Notification, validationSerivce, dataService) {
$rootScope.pageName = '1-search';
$scope.type = 'delivery';
$scope.searchZip = function() {
//vars
var zip = $scope.zip;
var type = $scope.type;
//check empty zip
if (typeof zip === "undefined" || zip === null){
Notification.error({message: "no zipcode entered", delay: 3000});
} else {
//validate zip
if (typeof validationSerivce.validateZip(zip) == "string") {
Notification.error({message: validationSerivce.validateZip(zip), delay: 3000});
} else {
//set spinner and blur
$scope.setBlur('add');
$scope.setSpinner('show');
//get the data
console.log(dataService.getSearchData(zip,type));
}
}
};
});
and the service looks like this:
App.factory("dataService", function($http) {
return {
getSearchData: function(zip,type) {
//Get request
$http({
url: url+"/search?zip=" + zip + "&type=" + type,
method: 'GET',
headers : {'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8'}
}).success(function(data){
return data;
}).error(function(err){"ERR", console.log(err)});
}
};
});
but the return is 'undefined'
the api returns data.... how can i do this the best way?