Angular2-mentions-如何从角度4中的angular2-提及中向提到的用户添加URL链接

时间:2019-06-04 13:35:54

标签: javascript angularjs angular angular5

如何使用angular2-mentions向@mention用户添加URL RouterLink。

我的代码是:

<div class="col-sm-12">
    <input type="text" [mention]="contactNames" [mentionConfig]="{triggerChar:'@',maxItems:10}" class="form-control input-underline input-lg baselg" id=""  maxlength="100" placeholder="Enter a conversation title(Max 100 characters)" [ngModelOptions]="{standalone: true}" [(ngModel)]="postText"> 
</div>

如何添加此内容:

<a [routerLink]="['/URL']" [queryParams]="{id:post?.userId}">{{contactNames}}</a>

对此有何建议?

1 个答案:

答案 0 :(得分:1)

Finale我是用Pipe做的。

HTML:

<h4 class="post-sub-heading" [innerHTML]="post.shrtMessage | UserLink : {users: post.mentionedUsers, path: '/dentistpublicprofile'} | GeneralLink"> </h4>

<input type="text" [mention]="contactNames" [mentionConfig]="{triggerChar:'@',maxItems:10, labelKey:'name',format:'[@]'}" class="form-control input-underline input-lg baselg" id=""  maxlength="100" placeholder="Enter a conversation title(Max 100 characters)" [ngModelOptions]="{standalone: true}" [(ngModel)]="postText">

管道:

import { Pipe, PipeTransform } from '@angular/core';

@Pipe({
    name: 'UserLink'
})
export class UserLinkPipe implements PipeTransform {

    transform(value: any, args: { path: string, users: { name: string, id: string }[] }): any {
        args.users.forEach(mentionedUser => {
            const mentionedName = '@' + mentionedUser.name;
            const regx = new RegExp(mentionedName, 'g')
            value = value.replace(regx, (url: string) => {
                return `<a href="${args.path}?id=${mentionedUser.id}">${mentionedName}</a>`;
            })
        })
        return value
    }
}