Retrieving data out of post success from separate controller angularjs

☆樱花仙子☆ 提交于 2019-12-02 10:21:42

Its very easy.

Controller:

fileUpload.uploadFileToUrl (file,uploadUrl, {
            successCallBack: yourSuccessHandler,
            failureCallBack: yourFailureHandler
        });

function yourSuccessHandler(data) {
            $scope.data= data; 
        }

function yourFailureHandler() {
            console.log("Failed Messge printing from Controller"); 
        }

Service

myApp.service('fileUpload', [ '$http', function($http) {
        this.uploadFileToUrl = function(file, uploadUrl, options) {
            var fd = new FormData();
            fd.append('file', file);

            $http.post(uploadUrl, fd, {
                transformRequest : angular.identity,
                headers : {
                    'Content-Type' : undefined
                }
            })

            .success(function(data) {
                console.log("upload successful");
                if(options && options.successCallBack) {
                    return options.successCallBack(data);
                }
            })

            .error(function() {
                console.log("upload error");
                if(options && options.failureCallBack) {
                    return options.failureCallBack();
                }
            });
        }
    } ]);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!