javascript - Return Value back to controller in angular js -


i want send data controller service if size of file extents how can achieve here code.

myapp.service('fileupload', ['$http', function ($http) { this.uploadfiletourl = function (filedata, uploadurl) {    if (filedata.size > 50000000) {         var fd = new formdata();         fd.append('file', filedata);         $http.post(uploadurl, fd, {             transformrequest: angular.identity,             headers: { 'content-type': undefined }         })         .success(function () {          })         .error(function () {          });     }     else {        return "image size more 5mb";     }    }   }]); 

you should use $q service deferred execution of request -

myapp.service('fileupload', ['$http','$q', function ($http,$q) { this.uploadfiletourl = function (filedata, uploadurl) {    if (filedata.size > 50000000) {         var fd = new formdata();         fd.append('file', filedata);        var deferred = $q.defer();         $http.post(uploadurl, fd, {             transformrequest: angular.identity,             headers: { 'content-type': undefined }         })         .then(function (result) {           deffered.resolve(result);         },function(error) {            deffered.reject();         });       return deferred.promise;     }     else {        return "image size more 5mb";     }    }   }]); 

for more information,check below links-

https://docs.angularjs.org/api/ng/service/$q https://thinkster.io/a-better-way-to-learn-angularjs/promises


Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -