繁体   English   中英

角异步HTTP自动完成

[英]Angular async http autocomplete

我想用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”承诺有关,但是我不确定如何解决它。 任何帮助,将不胜感激。

您执行了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

您为什么不尝试一下呢? https://angular-ui.github.io/bootstrap/#/typeahead

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM