【问题标题】:Angular Typeahead filter from more than one field来自多个字段的 Angular Typeahead 过滤器
【发布时间】:2014-09-30 09:19:56
【问题描述】:

我有一个基于人名过滤的预输入。我的 person 对象有更多的字段,而不仅仅是姓名(至少 {name, surname}),我希望过滤器能够根据姓名和姓氏工作。

这是当前的预输入,基于angular-ui-bootstrap

<input type="text" id="friendInput" placeholder="Friend's name"
 typeahead="friend for friend in friendsList | filter:{name:$viewValue}"
 typeahead-template-url="friendDropdown.html" ng-model="chosenFriend" />

我曾尝试编辑typeahead 属性的filter 部分,但我想我没有找到正确的方法。正确的方法是什么?

【问题讨论】:

    标签: javascript angularjs angular-ui-bootstrap


    【解决方案1】:

    你可以在你的范围内声明一个过滤器并使用它

    <input type="text" id="friendInput" placeholder="Friend's name"
      typeahead="friend for friend in friendsList | filter:nameFilter"
      typeahead-template-url="friendDropdown.html" ng-model="chosenFriend" />
    

    并在你的控制器中声明

    $scope.nameFilter = function(friend) {
        var regex = new RegExp('' + $scope.chosenFriend, 'i');
        return regex.test(friend.name) || regex.test(friend.surname); // test on both field
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 1970-01-01
      • 1970-01-01
      • 2016-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多