5

I want to set href attribute of element depend on content of element from interpolation.

<div>
    <ul class="contactnav">
        <li *ngFor="let contactItem of contactItems" >
            <a class="{{ contactItem.iconBootStrap }}"  href=" {{ contactItem.contactForm.indexOf('@') }} !== -1? 'mailto:{{ contactItem.contactForm }}' : '#'" data-rel="external">
                {{ contactItem.contactForm }}
            </a>           
        </li>
    </ul>
</div>

How should I define condition in element to set href for value if contactItem.contactForm includes @ otherwise set for value '#'?

maciejka
  • 748
  • 2
  • 12
  • 35

2 Answers2

7

What you ask for is setting a property, not an attribute.

You can use

[href]="contactItem.contactForm.indexOf('@') !== -1 ? 'mailto: ' + contactItem.contactForm : '#'"

For conditional attribute binding see How to add conditional attribute in Angular 2?

Günter Zöchbauer
  • 558,509
  • 191
  • 1,911
  • 1,506
0

Try this,

  <a class="{{ contactItem.iconBootStrap }}" [href]="contactItem.contactForm.indexOf('@') !== -1? 'mailto: ' + contactItem.contactForm : '#'" data-rel="external">
      {{ contactItem.contactForm }}
  </a>       
Sajeetharan
  • 203,447
  • 57
  • 330
  • 376