我有一个用于添加评论的文本输入。用户可以在输入中键入@符号以显示用户列表。然后,他们可以选择用户将其添加到评论中。我需要能够根据@符号后面输入的文本过滤用户名。所以“@s”会过滤所有用户名和/或带有“s”的名字/姓氏。有没有办法只在显示用户列表时运行过滤器,并且只根据在@符号后面输入的输入进行过滤?这是我现在拥有的:
HTML / SLIM:
/ USER LIST
.user-list.list-group [ng-show="usersShow"]
a.user.list-group-item [ng-class="{active:$first}" ng-repeat="user in users" ng-click="addUserToComment(user.username)"]
strong
| {{user.username}}
| {{' : ' + (user | fullName)}}
/ COMMENT FORM
= form_for Comment.new, html: {action: nil, name: 'newCommentForm', 'ng-submit' => 'createComment($event, comment)'} do |f|
= f.text_field :text, required: true, class: 'form-control comment-text', placeholder: 'Add comment...',
'ng-model' => 'comment.text', 'ng-change' => 'showUserList(comment.text)', 'ng-trim' => 'false'
span.input-group-btn
button.btn.btn-primary type="submit" ng-disabled='newCommentForm.$invalid' Add Comment
JS:
$scope.showUserList = function(comment) {
var USERNAME_PATTERN = /(?:^|\s)@\S*$/;
$scope.usersShow = USERNAME_PATTERN.test(comment);
};