0

I am developing an Angular 4 application and want to display a column only if totalYears <= 7. I have a ngFor and ngIf in the th element. I am currently getting an error. How do I handle it?

<table class="table">
  <thead>
    <tr>
      <th></th>
      <!-- <ng-container *ngIf="yearList.length > 6">  -->
      <th *ngIf="totalYears <=7" *ngFor="let year of yearList"> {{year}}</th>
      <!-- </ng-container> -->
      <th>Remaining Years</th>
    </tr>
  </thead>
</table>
2
  • 1
    can you add the complete code Commented Feb 5, 2018 at 12:36
  • And what is the error message exactly? Commented Feb 5, 2018 at 12:37

1 Answer 1

3

Angular doesn't support more than one structural directive on the same element.

You have an ngFor and an ngIf too on your th element:

<th *ngIf="totalYears<=7" *ngFor="let year of yearList">{{year}}</th>

You can do something like this instead:

<ng-container *ngIf="totalYears<=7">
    <th *ngFor="let year of yearList">{{year}}</th>                                
</ng-container>

Source: https://angular.io/guide/structural-directives#one-per-element

Also: totalYears should be accessible in this template view. Try printing totalYears like {{totalYears}} for debugging, for example.

Sign up to request clarification or add additional context in comments.

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.