本文介绍了如何返回值从功能控制器在它的异步HTTP服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为下面的函数的GetValue 。它是一个AngularJS模块内连同控制器。我想呼吁单击事件在控制器调用另一个函数此功能。(我希望我很清楚)

功能:

 函数的GetValue(数据,$ HTTP){
            VAR值=不确定;
           $ HTTP({
                网址:/ myurl,
                方法:GET,
                params:一个{
                    TMP:Data.tmp,
                    pressure:数据pressure
                }
            })。成功(功能(数据,状态,头,配置){
                值= parseFloat(执行console.log(数据)); //控制台在这里工作
               返回值;
            });
        返回值;
        }

内部控制的 code

 值=的GetValue(FORMDATA,$ HTTP);
        警报(值); //未定义在这里。好像值永远不会改变。

我还没有得到戒备的价值,但控制台prinitng值。我需要帮助,如果有可能的两个问题。


  1. 如何可以我的价值从内成功更改,并返回到控制器?

  2. 有什么办法,我没有从控制器注入HTTP的功能?
    -----这将是很好,如果我能做到这一点的单元测试。


解决方案

在理想的情况想拉$ http服务了控制器,使一家工厂做的那些电话。

在出厂前均接受你想要发送和有它返回的承诺反馈给控制器的数据的功能。

像这样

回购

  app.factory(fooRepo,[$ HTTP功能($ HTTP){
    返回{
        的getValue:功能(数据){
            返回$ HTTP({
                方法:POST,
                网址:/ myUrl
            });
        }
    };
}]);

Serivce

  app.factory(富,[$ Q,fooRepo功能($ Q,fooRepo){
    返回{
        的getValue:功能(数据){
            变种推迟= $ q.defer();            fooRepo.getValue(数据)
            .success(功能(结果){
                //做一些工作
                deferred.resolve(结果);
            })
            .error(功能(错误){
                // 做一些工作
                deferred.reject(错误);
            });            返回deferred.promise;
        }
    }
}]);

下面是控制器

  app.controller(fooCtrl,[foo的,功能(富){
    foo.getValue(dataHere)
        。然后(功能(结果){
            //这里做什么
        });
}]);

I have the following function named getvalue. It is inside an AngularJS module together with a controller. I am trying to call this function on click event invoking another function in a controller.(I hope I am clear)

function:

  function getvalue(Data, $http) {
            var value=undefined;
           $http({
                url: /myurl,
                method: "GET",
                params: {
                    tmp: Data.tmp,
                    pressure: Data.pressure               
                }
            }).success(function (data, status, headers, config) {            
                value=parseFloat(  console.log(data));//console working here
               return value;
            });
        return value;
        }

Code inside Controller

 value= getvalue(formData, $http );
        alert(value);//undefined here. Seems like value is never changed.

I have not getting the value on alert but console is prinitng the value. I need help if possible for two issues.

  1. How can I change the value from inside success and return to controller?
  2. Is there any way that I don't have to inject http from controller to function? -----It would be nice if I can do that for unit testing.
解决方案

you would ideally want to pull the $http service out of the controller and make a factory to do those calls.

in the factory have a function that accepts the data you are wanting to send and have it return the promise back to the controller

something like this

Repo

app.factory("fooRepo", ["$http", function($http){
    return {
        getValue: function(data){
            return $http({
                method: "POST",
                url: "/myUrl"
            });
        }
    };
}]);

Serivce

app.factory("foo", ["$q", "fooRepo", function($q, fooRepo){
    return {
        getValue: function(data){
            var deferred = $q.defer();

            fooRepo.getValue(data)
            .success(function(results){
                //do some work
                deferred.resolve(results);
            })
            .error(function(error){
                // do some work
                deferred.reject(error);
            });

            return deferred.promise;
        }
    }
}]);

here is the controller

app.controller("fooCtrl", ["foo", function(foo){
    foo.getValue(dataHere)
        .then(function(results){
            // do something here
        });
}]);

Added Plunkr

这篇关于如何返回值从功能控制器在它的异步HTTP服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 19:39