我在预输入时遇到问题。我有一个带有人名和姓氏的字段,如果我输入A,我希望看到重点放在姓氏而不是名字的前导字符上。

这是一个例子:

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


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

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

最佳答案

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



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>

关于javascript - 预先输入Angular UI,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27953841/

10-17 03:00