Angularjs从$ http自动完成

Gid*_*don 60 autocomplete directive angularjs angular-http

我正在尝试编写一个自动完成指令,该指令使用$ http请求从服务器获取数据(不使用任何外部插件或脚本).目前它仅适用于静态数据.现在,我知道我需要将$ http请求插入到source:指令中,但是我找不到关于这个主题的任何好的文档.

http请求

$http.post($scope.url, { "command": "list category() names"}). 
            success(function(data, status) {
                $scope.status = status;
                $scope.names = data;    
            })
            .
            error(function(data, status) {
                $scope.data = data || "Request failed";
                $scope.status = status;   
            });
Run Code Online (Sandbox Code Playgroud)

指示

app.directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
        };
    });
Run Code Online (Sandbox Code Playgroud)

视图

<input auto-complete ui-items="names" ng-init="manualcat='no category entered'" ng-model="manualcat"> 
Run Code Online (Sandbox Code Playgroud)

那么,我如何正确地将这一切拼凑成Angular方式呢?

Jus*_*cha 44

我制作了一个autocomplete指令并将其上传到GitHub.它还应该能够处理HTTP-Request中的数据.

这是演示:http://justgoscha.github.io/allmighty-autocomplete/ 这里的文档和存储库:https://github.com/JustGoscha/allmighty-autocomplete

因此,基本上,promise当您想要从HTTP请求获取数据时,您必须返回一个,这在加载数据时会得到解决.因此,您必须$q在发出HTTP请求的位置注入service/directive/controller.

例:

function getMyHttpData(){
  var deferred = $q.defer();
  $http.jsonp(request).success(function(data){
    // the promise gets resolved with the data from HTTP
    deferred.resolve(data);
  });
  // return the promise
  return deferred.promise;
}
Run Code Online (Sandbox Code Playgroud)

我希望这有帮助.

  • 是否需要将所有数据提取到内存中?如果我有10M +变种怎么办?:) (4认同)

Uri*_*igo 36

使用angular-ui-bootstrap的类型头.

它对$ http和promises有很大的支持.此外,它根本不包括任何JQuery,纯粹的AngularJS.

(我总是喜欢使用现有的库,如果他们缺少某些东西来打开问题或拉取请求,那么再好一点创建自己的库)

  • 请记住,截至今天,angular-ui-bootstrap中的Typeahead与Angular 1.3不兼容 (8认同)

mad*_*ead 16

您需要ng-change在范围内编写具有功能的控制器.在ng-change回调中,您可以调用服务器并更新完成.这是一个存根(没有$http这是一个插件):

HTML

<!doctype html>
<html ng-app="plunker">
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
        <script src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.4.0.js"></script>
        <script src="example.js"></script>
        <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
    </head>
    <body>
        <div class='container-fluid' ng-controller="TypeaheadCtrl">
            <pre>Model: {{selected| json}}</pre>
            <pre>{{states}}</pre>
            <input type="text" ng-change="onedit()" ng-model="selected" typeahead="state for state in states | filter:$viewValue">
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

JS

angular.module('plunker', ['ui.bootstrap']);

function TypeaheadCtrl($scope) {
  $scope.selected = undefined;
  $scope.states = [];

  $scope.onedit = function(){
    $scope.states = [];

    for(var i = 0; i < Math.floor((Math.random()*10)+1); i++){
      var value = "";

      for(var j = 0; j < i; j++){
        value += j;
      }
      $scope.states.push(value);
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 我正在使用angular-ui作为`typeahead`指令(autocomplete本身). (3认同)
  • 谢谢你的回答.但是,正如我在我的问题中所说,我试图避免使用任何外部脚本.在您的回答中,您正在使用angular-ui引导脚本.我正在寻找一种纯粹的角度解决方案.干杯,吉登 (2认同)

lon*_*dox 5

在没有外部模块或指令的情况下,在angular或angularjs中执行此操作的最简单方法是使用列表和数据列表HTML5。您只得到一个json并使用ng-repeat来提供数据列表中的选项。您可以从ajax获取json。

在此示例中:

  • ctrl.query是您键入时输入的查询。
  • ctrl.msg是在占位符中显示的消息
  • ctrl.dataList是获取的json

然后您可以在ng-reapet中添加过滤器和orderby

!! 列表和数据列表ID必须具有相同的名称!

 <input type="text" list="autocompleList" ng-model="ctrl.query" placeholder={{ctrl.msg}}>
<datalist id="autocompleList">
        <option ng-repeat="Ids in ctrl.dataList value={{Ids}}  >
</datalist>
Run Code Online (Sandbox Code Playgroud)

UPDATE:是本机HTML5,但对浏览器和版本类型怀有戒心。检查一下:https : //caniuse.com/#search=datalist