我有我真的不明白:)角,承诺和异步功能
我写了一个角的服务,打电话给我的API,重新格式化结果,并把这个数据到另一个功能转变为角控制器的问题。 我这么做很多次,但今天出了点问题。
重新格式化,结果和访问控制器的数据不一样,我不知道(也许明白)为什么:d
这是服务的代码:
myApp.factory('apiService', function($http) {
var myService = {
getMunicipalityAsync : function(id) {
var promise = null;
promise = $http({
method: 'GET',
url: '/api2/cc/municipality/' + id
}).success(function(response) {
var r = {
'success': true,
'data': response.data
};
console.debug(r, 'return this');
return r;
}).error(function(data, status, headers, config) {
logError("[apiService], getMunicipalityAsync() error, with status: " + status);
});
return promise;
}
}
return myService;
});
这是代码放入角度控制器。
apiService.getMunicipalityAsync($scope.conf.geoarea).then(
function(d) {
console.debug(d, 'return from service');
}, function(error) {
alert('error');
});
调试DATAS是不一样的:(
感谢
根据规格,'then'应该返回一个新的承诺,所以我觉得你可以做类似'$返回HTTP(..)然后(transformOutputFunc);'。 – Alxandr