简体   繁体   English

以角度4有条件地设置属性

[英]Set attribute conditionally in angular 4

I want to set href attribute of element depend on content of element from interpolation. 我想设置元素的href属性取决于插值元素的内容。

<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 '#'? 如果contactItem.contactForm包含@否则设置为值'#',我应该如何在元素中定义条件以设置值的href?

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? 对于条件属性绑定,请参阅如何在Angular 2中添加条件属性?

Try this, 试试这个,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM