如何使用AngularJS实现自动完成TextBox?

时间:2016-03-01 09:26:32

标签: angularjs ionic-framework

我有一个文本字段,当我开始输入时,应该选择预定义的单词。

 <input type="text"></input>

我有一个项目列表。只有这应该表明。如何使它工作? 控制器跟随。

 angular.module('starter.controllers', [])
          .controller('AppCtrl', function($scope, $ionicModal, $timeout, authService, $state, $http,$ionicLoading)
        {
          $scope.loginSubmitted = false;
       $scope.myflag = false;
        $scope.User = {};
       $scope.toast = function(){
       $ionicLoading.show({
       template: 'wrong credentials'
   });
     $timeout(function() {
       $ionicLoading.hide();
   }, 1000);
     }

         $scope.doLogin = function() {

     $scope.loginSubmitted = true;
   $scope.loginstatus==0;
     authService.GetByUsername().success(function(data) {
     $scope.UserData = data;
    console.log($scope.UserData);
    for (var i = 0; i < $scope.UserData.length; i++) {
      if ($scope.UserData[i].UserName == $scope.User.UserName && $scope.UserData[i].Password == $scope.User.Password) {

        $scope.loginstatus=1;
        break;
      }
    }
    if($scope.loginstatus==1){
      $state.go('app.single')
    }
    else {
        console.log('wrong credentials');
        $scope.toast();
      }
  }).error(function(err) {
    console.log(err);
  });
}
}).controller('PlaylistsCtrl', function($scope) {

 }).controller('EmployeeCntrl', function($scope, $stateParams) {

    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];

     $scope.data = {};

     //$scope.data.date = new Date().toDateString();
   $scope.data.FromDate = new Date();
   $scope.employees = [{name: "vishnu"}, {name: "seenu"}];

  $scope.selectedEmployee = $scope.employees[0].name;

  $scope.projects = [{name: "crwhy"}, {name: "big in"}];
     $scope.selectedProject = $scope.projects[0].name;
     $scope.logdata = function(form) {
    console.log($scope.data);
  }
});

现在可以按照要求进行吗?

2 个答案:

答案 0 :(得分:2)

你有几个插件: -    http://ngmodules.org/modules/ngAutocomplete

我有一个简单的指令: -

控制器编码: -

spy()

html: -

function DefaultCtrl($scope) {
    $scope.names = ["john", "bill", "charlie", "robert", "alban", "oscar", "marie", "celine", "brad", "drew", "rebecca", "michel", "francis", "jean", "paul", "pierre", "nicolas", "alfred", "gerard", "louis", "albert", "edouard", "benoit", "guillaume", "nicolas", "joseph"];
}

angular.module('MyModule', []).directive('autoComplete', function($timeout) {
    return function(scope, iElement, iAttrs) {
            iElement.autocomplete({
                source: scope[iAttrs.uiItems],
                select: function() {
                    $timeout(function() {
                      iElement.trigger('input');
                    }, 0);
                }
            });
    };
});

用作资源 http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/flick/jquery-ui.css https://code.angularjs.org/1.0.0/angular-1.0.0.js

小提琴: -

http://jsfiddle.net/sebmade/swfjT/

答案 1 :(得分:1)

您可以使用Select2库。

例如:

var app = angular.module('myApp', []);
app.controller('listCtrl', function($scope) {
  $scope.selectedItem;
    $scope.list = [
  {value:"AL",name:"Alabama"},{value:"WY",name:"Wyoming"}
  ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.9/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2-rc.1/css/select2.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.2-rc.1/js/select2.min.js"></script>
<div ng-app="myApp" data-ng-controller="listCtrl">

<select style="width:200px" class="select2" data-ng-model="selectedItem" data-ng-options="item.name for item in list">
 
</select>
  </div>

<script>
$( document ).ready(function() {
    $(".select2").select2();
  
});
</script>