1

The data I work with (bosses[]) has a boss object with contains the key-value email which is an string. I want to create the anchor with that string in the HTML. Also note that there's a loop in HTML that allows to access to each boss in bosses[].

So how can I access to create an anchor with boss.email which it only exists in the HTML loop?

I've tried <a [href]=`"mailto: + boss.email"></a> but doesn't work.

the html:

<div  class="boss" *ngFor="let boss of bosses" >
    <div class="boss-text">
        <div class="boss-text-name">{{boss.name}} </div>
        <div>{{boss.email}}</div>
        <a [href]="mailto: + boss.email"></a>
    </div>
</div>

The component:

import { Component, Input, OnInit } from '@angular/core';
import { boss} from 'interfaces'
        
@Component({
  templateUrl: 'boss-cell.component.html',
  selector: 'boss-cell',
})
export class BossCellComponent implements  OnInit {
  constructor() {}
    
  bosses: any[] =  [{
    email:       '[email protected]',
    name:        'kennedy',
  }]
}
2
  • You can use string interpolation. like this <a [href]="mailto:" + boss.email></a> Commented Jun 21, 2022 at 17:30
  • String interpolation example - <a [href]=`mailto: ${boss.email}`></a>' Commented Jun 21, 2022 at 17:36

3 Answers 3

3

You're close! I think this is what you're looking for:

<div class="boss" *ngFor="let boss of bosses" >
    <div class="boss-text">
        <div class="boss-text-name">{{boss.name}} </div>
        <a [href]="'mailto:' + boss.email">{{ boss.email }}</a>
    </div>
</div>
Sign up to request clarification or add additional context in comments.

Comments

1

You can use interpolation as Suraj already mentionned in the comments, or bind to a function creating the string. Depending on weather you are going to link other elements to the mail, you should pick the cleanest option for you.

Template

<div class="boss" *ngFor="let boss of bosses; let i = index">
  <div class="boss-text">
    <div class="boss-text-name">{{ boss.name }}</div>
    <a [href]="getMail(i)">{{ boss.email }}</a>
  </div>
</div>

Script

bosses: any[] =  [{
  email:       '[email protected]',
  name:        'kennedy',
}]

getMail(index: number) {
  return 'mailto:' +  this.bosses[index].email
}

Comments

1

You need to update this line

<a [href]="'mailto:' + boss.email">{{ boss.email }}</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.