没能弄清楚承诺在角HTTP是如何工作的 [英] not able to figure out how promises work in angular http

查看:155
本文介绍了没能弄清楚承诺在角HTTP是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code,它通过自动完成指令作出HTTP调用从服务器检索用户名:

  VAR APP2 = angular.module('MyModule的',[]);app2.directive(自动完成,函数($超时){
    返回功能(范围,iElement,iAttrs){
        iElement.autocomplete({
            来源:函数(){
                //无法调用此像这样:$范围适用('的getSource');。我得到$正在进行中的错误已经适用
                变种名称= scope.getSource();
                。范围$适用();
                的console.log(地名);
                返回名称;
            },
            异步:假的,
            的minLength:3,
            选择:功能(){
                $超时(函数(){
                    iElement.trigger('输入');
                },0);
            }
        });
    };
});app2.controller('DefaultCtrl',函数($范围,$ HTTP){
    $ scope.getSource =功能(){
        返回$ http.get('/ AccountRequest / GetMatchingEmployeeNames?和matchstr ='+ $ scope.selected)
            。然后(功能(数据){
                变种名称= [];
                对于(VAR I = 0; I< data.length;我++){
                    names.push(数据[I] .Name点);
                }
                返回名称;
            });
    };
});

我试图使用自动完成源的承诺:因为有一个在HTTP方法的延迟,我得到一个空数组,否则。任何人都可以请告诉我,我怎样才能使它发挥作用?我不知道如何提取的名称数组走出承诺的功能传递给源:


解决方案

 来源:功能(要求,responseCallback){
            变种名称= [];
            VAR承诺= scope.getSource();
            promise.success(功能(数据){
                名称=数据;
                的console.log(地名);
                responseCallback(地名);
            })错误(函数(){
                的console.log(出现了错误);
                responseCallback(地名);
            });
        },

或者

 来源:功能(要求,responseCallback){
            变种名称= [];
            VAR承诺= scope.getSource();
            promise.then(功能(数据){
                名称=数据;
                的console.log(地名);
                responseCallback(地名);
            },函数(){
                的console.log(出现了错误);
                responseCallback(地名);
            });
        },

我写这本基于jQuery的自动完成文件它说的来源需要传递请求和响应回调函数的函数。

查看更多关于一般承诺在这里是如何工作的: http://docs.angularjs.org/ API / NG。$ q

看这是具体如何延长为$ http服务 HTTP://docs.angularjs。组织/ API / NG。$ HTTP

I have the following code that retrieves user names from the server by making an http call on an autocomplete directive:

var app2 = angular.module('MyModule', []);

app2.directive('autoComplete', function ($timeout) {
    return function (scope, iElement, iAttrs) {
        iElement.autocomplete({
            source: function () {
                //can't call this like so: scope.$apply('getSource'); I get "$apply already in progress error"
                var names = scope.getSource();
                scope.$apply();
                console.log(names);
                return names;
            },
            async:false,
            minLength: 3,
            select: function () {
                $timeout(function () {
                    iElement.trigger('input');
                }, 0);
            }
        });
    };
});

app2.controller('DefaultCtrl', function($scope, $http) {
    $scope.getSource = function() {
        return $http.get('/AccountRequest/GetMatchingEmployeeNames?matchStr=' + $scope.selected)
            .then(function (data) {
                var names = [];
                for (var i = 0; i < data.length; i++) {
                    names.push(data[i].Name);
                }
                return names;
            });
    };
});

I'm trying to use a promise in the autocomplete source: because there's a delay in http method and I get an empty array otherwise. Can anybody please tell me how I can make it work? I don't know how to extract the names array out of the promise function to pass to source:

解决方案

source: function (request,responseCallback) {
            var names = [];
            var promise = scope.getSource();
            promise.success(function(data){
                names = data;
                console.log(names);
                responseCallback(names);
            }).error(function() {
                console.log("some error occurred");
                responseCallback(names);
            });
        },

or maybe

source: function (request,responseCallback) {
            var names = [];
            var promise = scope.getSource();
            promise.then(function(data){
                names = data;
                console.log(names);
                responseCallback(names);
            },function() {
                console.log("some error occurred");
                responseCallback(names);
            });
        },

I'm writing this based on the jQuery autocomplete documentation which says the source takes a function that is passed the request and response callback function.

See more on generally how promises work here: http://docs.angularjs.org/api/ng.$q

See specifically how this is extended for the $http service http://docs.angularjs.org/api/ng.$http

这篇关于没能弄清楚承诺在角HTTP是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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