【问题标题】:Angular async http autocompleteAngular 异步 http 自动完成
【发布时间】:2015-06-14 15:07:28
【问题描述】:

我想用 Angular 创建一个自动完成功能,因为这是我第一次接触 Angular,所以我很困惑。

这是我的代码:

 MenuService.getAutocompleteData(term).then(function (response) {
            $scope.menuData = response.data;
        });

这就是我调用进行以下 http 调用的服务的方式:

return $http({
            url    : autocompletePath,
            method : "POST",
            data   : {
                term: term
            }
        }).success(function (response) {
            return response;
        });

问题是它似乎是同步的,当我输入快速字母时,我的浏览器冻结了。我看到它与那个“.then”承诺有关,但我不确定如何解决它。任何帮助,将不胜感激。

【问题讨论】:

    标签: javascript ajax angularjs autocomplete


    【解决方案1】:

    您执行了一个 HTTP 请求,这是正确的,但您尝试在成功时返回结果。相反,您应该尝试在成功处理程序中做需要做的事情。

    所以在你的服务中你会做这样的事情:

        return $http({
            url    : autocompletePath,
            method : "POST",
            data   : {
                term: term
            }
        }); //note that there is no success handler in your service, because what's supposed to happen when the request completes, is the controller's business, not the service's. The service should only know where to get the data
    

    你可以把你的控制器改成这样:

      MenuService.getAutocompleteData(term).success(function (response) {
            $scope.menuData = response.data;
        }); //note the success-handler
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2019-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-24
        • 2019-12-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多