Angular2-mentions - How I can add URL link to mentioned user from angular2-mentions in angular 4

Viewed 507

How to add URL RouterLink to @mention user using angular2-mentions.

My Code is:

<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>

How to add this one:

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

Any suggestion on this?

1 Answers

Finale I did it using 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">

Pipe:

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
    }
}
Related