Javascript angularjs中的异步调用
我有几个关于angular中异步函数的问题。我希望我的服务函数能够返回使用$http获得的数据,并在另一个函数中使用该数据。让我用当前代码演示一下:Javascript angularjs中的异步调用,javascript,angularjs,asynchronous,Javascript,Angularjs,Asynchronous,我有几个关于angular中异步函数的问题。我希望我的服务函数能够返回使用$http获得的数据,并在另一个函数中使用该数据。让我用当前代码演示一下: dashboard.servicesModule.service('SubscriptionService', function ($http, $q) { this.getImportantData= function (param1, param2) { var url = "my/url/with/paramete
dashboard.servicesModule.service('SubscriptionService', function ($http, $q) {
this.getImportantData= function (param1, param2) {
var url = "my/url/with/parameters?param1=param1¶m2=param2";
$http.get(url).then(function(response){
console.log("response");
console.log(response.data); // < --- this actually shows the data i need!!
return response.data.vsyData; <--- however, this returns undefined
}, function(error){
// some error thingy just in case
});
};
this.getSomeFunctionality = function(param1, param2, param3){
var importantdata= this.getImportantData(param1, param2);
// do some filtering on the data with the purpose of returning only what i need
console.log(importantdata); <--- undefined
};
this.getEvenMoreFunctionality = function(param1, param2, param3){
var importantdata= this.getImportantData(param1, param2);
// reusing getImportantData again!
console.log(importantdata); <--- undefined
};
});
dashboard.servicesModule.service('SubscriptionService',函数($http,$q){
this.getImportantData=函数(param1,param2){
var url=“my/url/with/parameters?param1=param1¶m2=param2”;
$http.get(url).then(函数(响应){
控制台日志(“响应”);
console.log(response.data);//<---这实际上显示了我需要的数据!!
return response.data.vsyData;尝试以下操作:
this.getImportantData= function (param1, param2) {
var url = "my/url/with/parameters?param1=param1¶m2=param2";
return $http.get(url);
};
this.getSomeFunctionality = function(param1, param2, param3){
this.getImportantData(param1, param2)
.then(function(response){
console.log(response.data.vsyData);
}, function(error){
// some error thingy just in case
});
};
this.getEvenMoreFunctionality = function(param1, param2, param3){
this.getImportantData(param1, param2)
.then(function(response){
console.log(response.data.vsyData);
}, function(error){
// some error thingy just in case
});
};
无法从具有异步调用的方法中获取数据。可以返回promise,然后从promise中获取数据。尝试以下操作:
this.getImportantData= function (param1, param2) {
var url = "my/url/with/parameters?param1=param1¶m2=param2";
return $http.get(url);
};
this.getSomeFunctionality = function(param1, param2, param3){
this.getImportantData(param1, param2)
.then(function(response){
console.log(response.data.vsyData);
}, function(error){
// some error thingy just in case
});
};
this.getEvenMoreFunctionality = function(param1, param2, param3){
this.getImportantData(param1, param2)
.then(function(response){
console.log(response.data.vsyData);
}, function(error){
// some error thingy just in case
});
};
你不能从一个有异步调用的方法中获取数据。你可以返回承诺,然后你可以从承诺中获取数据。对于一个异步请求,你不能使用imediate return语句,主javascript线程不会等待请求解析。我真的推荐使用Kris Kowal的Q pro的角度实现mises/延迟系统
function getSomething(){
var deferred = $q.defer();
$http.get('foo/bar')
.success(function(data){
$deferred.resolve(data);
});
return deferred.promise();
}
getSomething().then(function(data){
console.log(data);
});
或者您可以直接返回$http
承诺。如果您想删除。那么()
从控制器中,您可以与支持$q
承诺的解析
选项一起使用,并创建可自行解析数据的路由,所以您只需将它们注入控制器,而无需执行$q
过程
更新1-解决路由依赖关系的实际示例
$routeProvider.when('/items/list', {
templateUrl: '/templates/item/list',
controller: 'ListItemsCtrl',
resolve: {
items: function(ItemsService) {
return ItemsService.getItems(); // getItems returns a promise like 'return $http.get('/items');'
}
}
});
//controller
function ListItemsCtrl($scope, items){
//do something with your items
}
对于不能发出imediate返回语句的异步请求,主javascript线程不会等待请求解析。我建议使用Kris Kowal的Q promises/deferred系统的角度实现
function getSomething(){
var deferred = $q.defer();
$http.get('foo/bar')
.success(function(data){
$deferred.resolve(data);
});
return deferred.promise();
}
getSomething().then(function(data){
console.log(data);
});
或者您可以直接返回$http
承诺。如果您想删除。那么()
从控制器中,您可以与支持$q
承诺的解析
选项一起使用,并创建可自行解析数据的路由,所以您只需将它们注入控制器,而无需执行$q
过程
更新1-解决路由依赖关系的实际示例
$routeProvider.when('/items/list', {
templateUrl: '/templates/item/list',
controller: 'ListItemsCtrl',
resolve: {
items: function(ItemsService) {
return ItemsService.getItems(); // getItems returns a promise like 'return $http.get('/items');'
}
}
});
//controller
function ListItemsCtrl($scope, items){
//do something with your items
}
你没有在任何地方给日期赋值,所以它显然是未定义的。@muenchdo你是对的,我想在编辑代码时速度太快,把公司敏感数据漏掉。我要修改我的问题。你没有在任何地方给日期赋值,所以它显然是未定义的。@muenchdo你是对的,我不想o编辑代码时速度太快,无法将公司敏感数据保留在外。我将修改我的问题,因此基本上GetSomeFunction函数无法将最终结果(例如数据的一些数学验证和筛选)返回给我的控制器?我可以在.then(函数(响应){}中进行筛选但是我不能将结果返回到我的控制器?如果是这样,那么将该逻辑放在一个单独的服务中是没有意义的,我想?因此基本上GetSomeFunction函数不能将最终结果(例如一些数学验证和数据过滤)返回到我的控制器?我可以在.then(函数(响应){}中进行过滤但是我不能将结果返回给我的控制器?如果是这样,我想将该逻辑放在单独的服务中是没有意义的?谢谢你的更新!这看起来非常有用!谢谢你的更新!这看起来非常有用!请在你的回答中添加一点解释请在你的回答中添加一点解释