【问题标题】:Angular UI Bootstrap Typeahead ignore fieldAngular UI Bootstrap Typeahead 忽略字段
【发布时间】:2014-04-26 17:01:41
【问题描述】:

我正在使用AngularUI-Bootstraptypeahead

<input type="text" name="recipient" id="recipient" placeholder="Friend's name or username"
 autofocus="autofocus"
 class="form-control"
 ng-model="payment.recipient"
 typeahead="friend.username for friend in friends | filter:$viewValue | limitTo:4"
 typeahead-template-url="typeaheadTemplate.html"
 typeahead-editable="false"
 ng-required="true"/>

每个 Friend 对象具有以下结构:

{
    name: friendship.name,
    username: friendship.username,
    picture: friendship.picture // Url
}

允许用户按图片搜索(它是头像的url)没有意义,并且修改过滤器没有成功。

我尝试过的:

typeahead="friend.username for friend in friends| filter:{username:username, name:name} | filter:$viewValue | limitTo:4"
typeahead="friend.username for friend in friends | filter:{username:$viewValue, name:$viewValue} | limitTo:4"

在过滤器表达式上使用$viewValue 两次似乎会破坏它...有什么想法吗?

【问题讨论】:

标签: angularjs angular-ui-bootstrap


【解决方案1】:

试试这个:

typeahead="friend.username for friend in myFilterFunc($viewValue) | limitTo:4"

在你的控制器中:

$scope.myFilterFunc = function(viewValue) {
  var filteredFriends = [];
  angular.forEach($scope.friends, function(friend) {
    if ((friend.username.indexOf(viewValue) > -1) || 
       (friend.name.indexOf(viewValue) > -1)) 
       {
         filteredFriends.push(friend);
       }
  });
  return filteredFriends;
};

【讨论】:

  • 请说得更具体些,什么不完全有效?它在我的 plnker 中工作。
猜你喜欢
  • 2015-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多