Set href in attribute directive in Angular

后端 未结 3 790
醉梦人生
醉梦人生 2020-12-15 17:54

I\'m trying to set up a Bootstrap tab strip with Angular 2. I have the tabs rendering in an ngFor but I\'m getting template errors when I try to put the

相关标签:
3条回答
  • 2020-12-15 17:59

    Why don't you use routerLink instead of href? I think, this will work

    <a routerLink="#{{ aType.Name }}">Name<a>
    
    0 讨论(0)
  • 2020-12-15 18:04

    There is no need to prefix with #

    In this code

    <ul class="nav nav-tabs" role="tablist">
      <li *ngFor="let aType of resourceTypes; let i = index"
          [ngClass]="{'active': i == 0}"
          role="presentation">
        <a [attr.href]="aType.Name"
           [attr.aria-controls]="aType.Name"
           role="tab"
           data-toggle="tab">
          {{aType.Name}}
        </a>
      </li>
    

    aType already refers to the *ngFor variable.

    If you want to prefix a literal # you can use

    [attr.href]="'#' + aType.Name" 
    

    There is also no need for attr. if the element actually has the property href which is the case with <a>.

    0 讨论(0)
  • 2020-12-15 18:14

    Well you can bind it with string interpolation:

    href = "#{{aType.Name}}" 
    

    (Note that the attribute used here is href, not [attr.href].)

    0 讨论(0)
提交回复
热议问题