【发布时间】:2017-04-10 19:34:35
【问题描述】:
app.service('cityService',['$http',function($http){
this.get = function(){
$http({
method : 'get',
url : API_SERVER+'city/'
}).success(function(response){
console.log(response);
return response;
}).error(function(response){
console.log(response);
});
};
}
我正在尝试使用该服务从服务器获取数据,但问题是当我使用该服务将数据存储在范围变量中时。我得到undefined。我正在接收数据,因为数据显示在控制台中+当我添加控制器时服务也在工作,但数据没有保存在任何变量中。我使用$timeout 来延迟检查变量,但仍然没有运气。我也使用了.then(),但是当我添加服务时它显示undefined 服务......
app.controller('Controller',['cityService',function(cityService){
$scope.temp = {};
$scope.temp = cityService.get();
}]);
无论如何要将数据从服务传递到控制器? 笔记: 还尝试将数据存储到服务变量中并返回服务变量
this.var = ''
this.get = function(){
$http({
method : 'get',
url : API_SERVER+'city/'
}).success(function(response){
this.var = response
console.log(this.var);
return this.var;
}).error(function(response){
console.log(response);
});
};
数据存储到变量中,但最终没有传递给控制器。 有什么指导吗?
【问题讨论】:
标签: javascript angularjs asynchronous angularjs-service