marek marek - 1 year ago 100
AngularJS Question

Angular $ value from promises

I would like to ask you how to send data through $ which I get from the function using $q.defer().

Here is the bit of code:


<input type='text' ng-model='name'/>
<input type='file' id='inputFile' />
<button ng-click='save'>Save</button>


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 =;
return deferred.promise;

$rootScope.result = result;

Post request

$, {
name: $,
image: $rootScope.result
}).success(function(data) {

The problem is, that when i hit save, then content of 'name' is sent, but the $rootScope.result is empty. Even though when I put $rootScope.result to console.log() I can see the result, but in the post it is empty?
What can be possibly doing wrong?

Answer Source

I don't know if this is what you want:

$ = function () {
    imgToBase64(file).then(function (result) {
        $rootScope.result = result;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download