【发布时间】:2014-06-23 08:50:18
【问题描述】:
我已经阅读了有关“promise”对象以及获取某种异步调用或等到 http 调用完成的所有方法,但我未能成功。这就是我得到的,也是我想做的:
我需要从我的服务器获取一些 json 文件,并在我的代码(js 文件)中使用来自该 json 的数据,而不仅仅是作为我的 HTML 模板的数据。
我有一个调用 json 文件的服务:
mobilityApp.service('serveiWebservices', function($resource) {
return {
getWS: function($scope) {
var path = 'jsonWS/context.json';
$resource(path).get(function (data) {
console.log(data); //data is printed fine
$scope.returnData = data; //just to test, it doesn't work neither
return data;
});
}
};
});
从我的控制器我这样称呼它:
var data = serveiWebservices.getWS($scope);
console.log(data); //undefined
关于如何使用函数返回的承诺对象并在获得请求的数据后立即执行操作的任何想法?我知道我可以设置一个“成功”函数,但我不想使用回调。
Tnanks 提前!
【问题讨论】:
标签: json angularjs asynchronous service promise