Skip to content Skip to sidebar Skip to footer

Get Data From A Callback And Save It To A Variable In Angularjs

Following is my service code, which is saving an image - angular.module('app').service('fileUpload', ['$http', function ($http) { return { uploadFileToUrl: function(file,

Solution 1:

It's async so you should do it with a callback function.

angular.module("app").service('fileUpload', ['$http', function ($http) {
    return {
      uploadFileToUrl: function(file, uploadUrl, successCb, errorCb){
        var fd = new FormData();
        fd.append('file', file);
        $http.post(uploadUrl, fd, {
            transformRequest: angular.identity,
            headers: {'Content-Type': undefined}
        })
        .success(function(response){
            if(successCb){
                successCb(response);
            }
        })
        .error(function(){
            if(errorCb){
                errorCb();
            }
        });        
      }
    }

}]);

Post a Comment for "Get Data From A Callback And Save It To A Variable In Angularjs"