5
<li *ngFor="let year of paper?.years" class="list-group-item">                                                
                    <div>
                        <a routerLink="/viewpdf/{{year.questionPaper.fileId}}">Question Paper - {{year.formattedYear }}</a> 
                        <a href="{{ downloadUrl }}/{{year.questionPaper.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>
                    <div> 
                        <a routerLink="/viewpdf/{{year.markScheme.fileId}}"> || Mark Scheme - {{year.formattedYear }}</a>                        
                        <a href="{{ downloadUrl }}/{{year.markScheme.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>   
</li>

The above code is working fine. But I want to check each fileId, if its null or not. If null then the links will not generate and vice versa.

I have setted *ngIf, below is a code sample. But its not working.

<li *ngFor="let year1 of paper?.years" class="list-group-item">  

                    <div *ngif="year.questionPaper.fileId != ''">
                        <a routerLink="/viewpdf/{{year.questionPaper.fileId}}">Question Paper - {{year.formattedYear }}</a> 
                        <a href="{{ downloadUrl }}/{{year.questionPaper.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>
                    <div *ngif="year.markScheme.fileId != ''"> 
                        <a routerLink="/viewpdf/{{year.markScheme.fileId}}"> || Mark Scheme - {{year.formattedYear }}</a>                        
                        <a href="{{ downloadUrl }}/{{year.markScheme.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>                      

                </li>

I have tried several other ways, but nothing is working. Like : defining ng-template.

scipper
  • 2,944
  • 3
  • 22
  • 45
Abdur Rahim
  • 3,975
  • 14
  • 44
  • 83

4 Answers4

6

No need to use == or !=.

Just modify your code with the below code.

<li *ngFor="let year1 of paper?.years" class="list-group-item">      
     <div *ngIf="year.questionPaper.fileId">
        <a routerLink="/viewpdf/{{year.questionPaper.fileId}}">Question Paper - {{year.formattedYear }}</a> 
        <a href="{{ downloadUrl }}/{{year.questionPaper.fileId}}" class="fa fa-download" aria-hidden="true"></a>
     </div>
     <div *ngIf="year.markScheme.fileId"> 
        <a routerLink="/viewpdf/{{year.markScheme.fileId}}"> || Mark Scheme - {{year.formattedYear }}</a>                        
        <a href="{{ downloadUrl }}/{{year.markScheme.fileId}}" class="fa fa-download" aria-hidden="true"></a>
    </div>                        
</li>

Explanation:

<div *ngIf="year.questionPaper.fileId"> // This will automatically check for null, undefined and empty string.

Surjeet Bhadauriya
  • 6,755
  • 3
  • 34
  • 52
3

You have a typo. use *ngIf

<div *ngIf="year.questionPaper.fileId != ''">
                        <a routerLink="/viewpdf/{{year.questionPaper.fileId}}">Question Paper - {{year.formattedYear }}</a> 
                        <a href="{{ downloadUrl }}/{{year.questionPaper.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>
                    <div *ngIf="year.markScheme.fileId = ''"> 
                        <a routerLink="/viewpdf/{{year.markScheme.fileId}}"> || Mark Scheme - {{year.formattedYear }}</a>                        
                        <a href="{{ downloadUrl }}/{{year.markScheme.fileId}}" class="fa fa-download" aria-hidden="true"></a>
                    </div>  
Syedur
  • 414
  • 4
  • 10
3

An extension to Syedur answer,

*ngIf="year.markScheme.fileId = ''" in the above line you missed == or === but not =. Can be =

Mr_Perfect
  • 8,254
  • 11
  • 35
  • 62
1

Surjeet Bhadauriyas answer led me to the right path with my problem. Still i wanna reference this question with following error: "Cannot read property name of undefined", because searching for this error brought me to mostly async data problems, which this is not:

The array has more data points than the dictionary, i want to iterate (ngFor) over the array and access the dictionary except (ngIf) when there is no datapoint.

component

letters: string[] = ["A", "B", "C"];
dictionary: { [key: string]: { name: string } } = {};

ngOnInit() {
    this.dictionary["A"] = { name: "Alpha" };
    this.dictionary["B"] = { name: "Beta" };
}

template (does not work)

<ng-container *ngFor="let letter of letters">
    <ng-container *ngIf="letter!=C">
        <pre>{{letter}}</pre>
        <pre>{{dictionary[letter].name}}</pre>
    </ng-container>
</ng-container>

template (does work)

<ng-container *ngFor="let letter of letters">
    <ng-container *ngIf="dictionary[letter]">
        <pre>{{letter}}</pre>
        <pre>{{dictionary[letter].name}}</pre>
    </ng-container>
</ng-container>
connectedMind
  • 421
  • 4
  • 17