从单独的控制器 angularjs 中检索成功后的数据 [英] Retrieving data out of post success from separate controller angularjs

查看:31
本文介绍了从单独的控制器 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 显示信息.我现在的挣扎是将数据拉到这个成功函数之外,更不用说服务本身了.我尝试在服务外部创建一个全局变量名称和地址,以便我可以从另一个控制器访问它,但它显示为未定义.

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 中检索成功后的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆