Can you help me figure out how to successfully send data through $http.post that I receive from a function using $q.defer()? Here is the code snippet:
HTML
<input type='text' ng-model='name'/>
<input type='file' id='inputFile' />
<button ng-click='save'>Save</button>
JavaScript
file = document.getElementById('inputFile');
function imgToBase64(file_input) {
var deferred = $q.defer();
if (file_input.files && file_input.files[0]) {
var fileReader = new FileReader();
fileReader.onload = function (event) {
result = event.target.result;
deferred.resolve(result);
};
fileReader.readAsDataURL(file_input.files[0]);
}
return deferred.promise;
};
imgToBase64(file).then(function(result){
$rootScope.result = result;
});
Post request
$http.post(apiURL, {
name: $scope.name,
image: $rootScope.result
}).success(function(data) {
console.log(data);
})
When I click save, only the content of 'name' gets sent and $rootScope.result remains empty. Interestingly, when I log $rootScope.result it does show the result, but it appears empty in the post request. Any idea what could be wrong? Thank you.