Search code examples
angularjsangularjs-scopeangular-file-upload

How to pass data from function to angular controller scope


I stuck at this problem for days.
I use angular file upload to upload image. I can upload the image to the server, and I can get the response image url from the server back.

But I can't get the response image url into my controller's $scope, therefore I cannot preserve the url to the outer controller.

The following is part of my code.

.controller('AppController', ['$scope', 'FileUploader',  function ($scope, FileUploader) {
    $scope.uploader = new FileUploader({
        url: 'http://www.example.com/upload',
    });
    $scope.imgurl = {};   // I want to put the response url here. 

    $scope.answer = function (answer) {
        $mdDialog.hide(answer);
    };


    $scope.uploader.onCompleteItem = function (fileItem, response, status, headers) {
        console.info('onCompleteItem', fileItem, response, status, headers);
        console.info(response.name); // here I can get the response url

    };

}]);

Thanks in advance.


Solution

  • Just assign the value,

     $scope.uploader.onCompleteItem = function (fileItem, response, status, headers) {
            console.info('onCompleteItem', fileItem, response, status, headers);
            console.info(response.name); // here I can get the response url
             $scope.imgurl = response.name; 
    };