引导AngularJS uib-typeahead上的异步请求 [英] Asynchronous request on bootstrap AngularJS uib-typeahead

查看:130
本文介绍了引导AngularJS uib-typeahead上的异步请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在服务器端搜索用户输入的名称,并使用引导程序3提供建议. uib-typeahead ,但是(尽管我对值进行了硬编码以返回-用于测试目的),但没有值返回到typeahead下拉列表.我的猜测是,由于这是一个异步请求,因此在检索请求数据时已经返回了任何数据.

I am trying to search on the server side a name entered by the user, and give suggestions using bootstrap 3 uib-typeahead, however (although I hard coded the values to return - for testing purposes), no value is returning to the typeahead dropdown. My guess is that since this is an async request, any data is already returned by the time the request data is retrieved.

在预输入监听期间,有什么方法可以从服务器端检索数据吗?

Is there any way how data could be retrieved from server side, whilst typeahead is listening?

$scope.test_search = function(test_name){
    var curr_url = '/plan/ajax/test/';

    var search_response =  $http.get(curr_url,{
        cache: true,
        params:{'name': test_name, 'csrfmiddlewaretoken': $cookies.csrftoken}
    }).then(function successCallback(response) {
        console.log(response.data);
        //return response.data;
        var test = [
            {country: "US", name : 'Alabama'}
        ];
        return test;

    }, function errorCallback(response) {
        alert('Error');
    });

},

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="col-lg-6">
    <pre>Model: {{new_ingredient_selected | json}}</pre>
    <input type="text"
           ng-model = "new_ingredient_selected"
           placeholder="test test"
           uib-typeahead="ingredient.name for ingredient in test_search($viewValue) |  filter:{name:$viewValue}"
           typeahead-loading="loadingLocations"
           typeahead-no-results="noResults"
           typeahead-on-select="onTypeaheadIngredientSelect($item, $label, $index)"
           typeahead-wait-ms ="400"
           class="form-control">

    <i ng-show="loadingLocations" class="glyphicon glyphicon-refresh"></i>
    <div ng-show="noResults">
      <i class="glyphicon glyphicon-remove"></i> No Results Found
    </div>
</div>

推荐答案

test_search 函数需要

当函数缺少返回语句,它将返回 undefined 的值.

When a function lacks a return statement, it returns a value of undefined.

另外,重新抛出拒绝处理程序中的错误响应.否则,被拒绝的承诺将被转换为已实现的承诺,并以 undefined 的值进行解析.

Also it is important to re-throw the error response in the rejection handler. Otherwise the rejected promise will be converted to a fulfilled promise that resolves with a value of undefined.

这篇关于引导AngularJS uib-typeahead上的异步请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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