Angular UI typeahead始于

时间:2015-01-14 22:44:38

标签: javascript angularjs angular-ui-bootstrap angular-ui-typeahead

我有类型的问题。我有一个名字和姓氏的字段,如果我输入A,我希望看到关注姓氏而不是名字的主要字符。

这是一个例子:

 function TypeaheadCtrl($scope) {
        $scope.selected = undefined;
        $scope.Person = ['Jane Smith', 'John Smith', 'Sam Smith', 'John Doe','Daniel Doe'];
    }

当我输入S时,我想只看到简史密斯和约翰史密斯。有办法做到这一点??

plunker:http://plnkr.co/edit/inwmqYCCRsjs1G91Sa3Q?p=preview

2 个答案:

答案 0 :(得分:1)

我假设您希望 sourceArray 中找到的每个列出的项目都只为姓氏突出显示搜索字词。如果不修改指令本身,这是不可能的,但我有一个替代解决方案,虽然它也突出了名字中的搜索词(如果匹配), ONLY 呈现姓氏与搜索词匹配的人的结果。我希望这会有所帮助:



angular.module("firstChar", ["ui.bootstrap"]);

angular.module("firstChar").controller("TypeaheadCtrl", function($scope, $filter) {
  $scope.selected = undefined;
  
  // ==========================================================
  // You would have to replace the JSON assignment code below
  // with a call to $http.get, to get that file you talked
  // about in your comment below:
  //
  // $http.get('OutAnagrafica.json').success(function (data) {
  //    $scope.OutAnagrafica = data;
  // });
  //
  // ==========================================================
  
  $scope.OutAnagrafica = [
    {
        "Name": "Jane Smith"
    },
    {
        "Name": "John Smith"
    },
    {
        "Name": "Sam Smith"
    },
    {
        "Name": "Sam Northrop"
    },
    {
        "Name": "John Doe"
    },
    {
        "Name": "Daniel Doe"
    }
  ];
  
  $scope.persons = $scope.OutAnagrafica.map(function (person) {
    var nameParts = person.Name.split(" "),
        name = nameParts[0],
        surname = nameParts.slice(1).join(" ");
    
    return {
      "name": name,
      "surname": surname
    };
  });
  
  $scope.getPersonsFromSurnames = function(searchTerm) {
    return $filter("filter")($scope.persons.map(function (person) {
      return {
        "fullname": person.name + " " + person.surname,
        "surname": person.surname
      };
    }), {
      "surname": searchTerm
    });
  }
});

<link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.0/ui-bootstrap-tpls.min.js"></script>
<div ng-app="firstChar">
  <div class="container-fluid" ng-controller="TypeaheadCtrl">
    <div>Selected: <span>{{selected}}</span>
    </div>
    <div>
      <input type="text" ng-model="selected" typeahead="person.fullname for person in getPersonsFromSurnames($viewValue)">
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

来自http://angular-ui.github.io/bootstrap/#/typeahead, 看着这条特殊的线

<input type="text" ng-model="asyncSelected" placeholder="Locations loaded via $http" typeahead="address for address in getLocation($viewValue)" typeahead-loading="loadingLocations" class="form-control">

您可以执行类似

的操作

<input type="text" ng-model="selected" typeahead="name for name in getPersonLastName($viewValue)">

这意味着您有一个getPersonLastName函数,该函数查看$ scope.Person并返回与姓氏匹配的名称和$ viewValue。