Angularjs autocomplete from $http

前端 未结 5 2146
-上瘾入骨i
-上瘾入骨i 2020-11-29 16:52

I\'m trying to write an autocomplete directive that fetches data from the server using an $http request (without using any external plugins or scripts). Cur

5条回答
  •  不知归路
    2020-11-29 17:16

    You need to write a controller with ng-change function in scope. In ng-change callback you do a call to server and update completions. Here is a stub (without $http as this is a plunk):

    HTML

    
    
        
            
            
            
            
        
        
            
    Model: {{selected| json}}
    {{states}}

    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);
        }
      }
    }
    

提交回复
热议问题