与 angularjs 中的 http 数据共享服务
Share service with http data in angularjs
我不想跨控制器执行相同的 http 调用,所以我想创建一个服务来获取数据并将 returns 数据发送到我尝试这样做的控制器
控制器:
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));
}
}
};
});
服务如下所示:
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)});
}
};
});
但是 return 是 'undefined'
apireturns 数据....我怎样才能最好地做到这一点?
您的 getSearchData()
函数中没有 return 语句。 Return 来自 $http 的结果并将其作为承诺访问。例如
dataService.getSearchData(zip,type).then(function(response) {
console.log(response)
});
试试这个:
App.factory("dataService", function($http) {
return {
getSearchData: function(zip,type) {
//Get request
return $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)});
}
};
});
我不想跨控制器执行相同的 http 调用,所以我想创建一个服务来获取数据并将 returns 数据发送到我尝试这样做的控制器
控制器:
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));
}
}
};
});
服务如下所示:
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)});
}
};
});
但是 return 是 'undefined'
apireturns 数据....我怎样才能最好地做到这一点?
您的 getSearchData()
函数中没有 return 语句。 Return 来自 $http 的结果并将其作为承诺访问。例如
dataService.getSearchData(zip,type).then(function(response) {
console.log(response)
});
试试这个:
App.factory("dataService", function($http) {
return {
getSearchData: function(zip,type) {
//Get request
return $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)});
}
};
});