首页 > 解决方案 > 单击任何字母时的角度提及触发器

问题描述

在我的 angular 项目中,我使用 angular-mention 库来建议用户名。@如果我输入或,它工作正常#。只有当我输入@或#时,它才会给我用户列表。

但我不想输入@or #。我想输入任何字母,这样它就会给我所需的结果。假设如果我输入 j,那么它将给我所有以字符 J 或 j 开头的用户名列表。

谁能帮我这个?

您可以在此处参考此工作代码...链接。

上面的链接是我可用的工作示例,但它仅在我输入 @ 时才有效,我想在输入任何字符时工作

另外,我尝试如下

<input type="text" class="searchBox" [mention]="items" [mentionConfig]="{triggerChar:''}" placeholder="Search user" [(ngModel)]="searchText" />

在上面的代码triggerChar中,如果我输入@或#,我需要输入一些值,它会按照它工作。但是为任何我不知道该怎么做的角色工作?

标签: angularmention

解决方案


您可以通过修改库来做到这一点。我认为这是不应该推荐的臭脏黑客。但如果你真的想要它...

angular-mentiontriggerChar用作索引提及列表的键。

// in file "angular-mentions/src/lib/mention.directive.ts"

let config = this.triggerChars[charPressed]; <= locate this line

/* custom code start */
const yourTriggerCharList = ['a', 'b', ...
if (yourTriggerCharList.indexOf(charPressed) >= 0 && !config && !this.searching) {
    config = this.triggerChars[anExistingTriggerChar];
}
/* custom code end */

if (config) { // <= triggerChar found
    ... (showing search list)

推荐阅读