Hi all,
while im checking my code i found some difference usings of $http.get() - Whats the correct way?
#1:
var getProfiles = function () {
var deferred = $q.defer();
$http.get(cordova.file.dataDirectory + theFile)
.success(function(data, status, headers, config) {
deferred.resolve(data);
})
.error(function(data, status, headers, config) {
deferred.reject({status: “ERROR”});
});
return deferred.promise;
}
#2:
var getProfiles = function () {
var deferred = $q.defer();
$http.get(cordova.file.dataDirectory + theFile).then(
function(success) {
deferred.resolve(success)
},
function(error) {
deferred.reject(error);
}
);
return deferred.promise;
}
it is the same… but different… but still the same!
success and error are only mappings for the success and error calback for the promise.
and by the way --> promises are chainable -> you do not need to define your own promise… so you can do something
var getProfiles = function () {
return $http.get(cordova.file.dataDirectory + theFile).then(
function(success) {
return success;
}
);
}
or you simply return the call of the get-method:
var getProfiles = function () {
return $http.get(cordova.file.dataDirectory + theFile);
}
Ahhh good to know. Thank you @bengtler
Maybe its better to simply return the call if i use it in a service and handle the result over the controller, right?
yep. Because you do not make any transformation of the request-result.