从单独的控制器angularjs检索成功后的数据

从单独的控制器angularjs检索成功后的数据

本文介绍了从单独的控制器angularjs检索成功后的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的服务,该服务上传一个文件并将其发布到Spring控制器.控制器处理此数据,并以JSON形式返回几个对象.我正在使用以下Angular服务:

I am writing a simple service that uploads a file and posts it to a Spring controller. The controller manipulates this data and returns several objects as JSON.I am working with the following Angular service:

myApp.service('fileUpload', [ '$http', function($http) {
        this.uploadFileToUrl = function(file, uploadUrl) {
            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")
                //console.log(data)
                namesandaddresses = data;
            })

            .error(function() {
                console.log("upload error")
            });
        }
    } ]);

我在代码中有一个单独的控制器,该控制器用于将此数据(名称和地址)发布到视图并使用ng-repeat显示信息.我现在的挣扎是将数据拉到成功功能之外,更不用说服务本身了.我尝试在服务之外创建一个全局var名称和地址,以便可以从另一个控制器访问它,但是它显示为未定义.

I have a separate controller in the code that I want to use to post this data (namesandaddresses) to the view and use ng-repeat to display the information. My struggle right now is pulling the data outside of this success function, let alone the service itself. I have tried creating a global var namesandaddresses outside the service so that I can access it from another controller but it shows up as undefined.

从我的研究中,我发现执行此操作的适当方法是使用回调函数,但是我尝试将此函数写入服务破坏了基本功能.在退一步以重新设计此服务或重新考虑我的方法之前,我想发布此内容,看看stackoverflow社区是否可以帮助我调整此服务,以便我可以将该JSON发布到成功的视图中.

From my research I have found the appropriate way to do this is to use a callback function but my attempts to write this into the service breaks the basic function. Before stepping back to redesign this service or reconsidering my approach I wanted to post this and see if the stackoverflow community could help me tweak this service so that I can post this JSON to the view upon success.

有人会帮我吗?

推荐答案

非常简单.

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

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

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

服务

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();
                }
            });
        }
    } ]);

这篇关于从单独的控制器angularjs检索成功后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 15:06