-1

Im trying to show a div if "avenant" does not exist but its not working !

*ngIf="avenant" is working it shows the avenants if they exist, but *ngIf="!avenant" doesn't show the message in the div !

  <ng-container *ngFor="let avenant of Avenants" >
      <ng-container  *ngIf="avenant">

      <div class="panel-body border border-purple rounded stl2">
        <table class="table table-hover">
          <thead class="thead-light" >
            <tr>
              <th>id</th>
             <th> Actions</th>
            </tr>
          </thead> 
          <tbody>
            <tr  *ngFor="let avenant of Avenants | orderBy: 'id' ">
              <td>{{avenant.id}}</td>
              <td>
                <button class="btn btn-danger" type="button" (click)="deleteAvenantId(avenant.id)> </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
     </ng-container>
    <ng-container *ngIf="!avenant">
      <div>
       no avenant found !
      </div>
    </ng-container>
   </ng-container>
Lamyae Lac
  • 43
  • 11
  • Why not use `else`, see e.g. https://angular.io/api/common/NgIf – jonrsharpe Jun 19 '20 at 15:49
  • what is avenant? an object, a boolean? – ukn Jun 19 '20 at 15:49
  • you could use *ngIf="!avenant" if it is boolean. – Suraj Gupta Jun 19 '20 at 15:51
  • its an object @ukn – Lamyae Lac Jun 19 '20 at 15:55
  • @LamyaeLac if its an instance of an object it will never be undefined which is why the !avenant doesnt work. If your array is an array of avenant and doesnt allow undefined, it will never work like that. The better way to do what you are trying to do is using the else but it doesnt help you understand anything here. If avenant is an object, the only way a negative statement can work is if the object === undefined or null. {} is not going to work. – ukn Jun 19 '20 at 16:00
  • 1
    Hope this helps you, this might resolve your query [link](https://stackoverflow.com/questions/37111005/how-to-check-empty-object-in-angular-2-template-using-ngif) – Suraj Gupta Jun 19 '20 at 16:01
  • @LamyaeLac provide us the Avenants array as a json. The solution is super simple but since we dont know what your data look like its hard to give you the right answer. – ukn Jun 19 '20 at 17:13

3 Answers3

0

Rewrite your code

<ng-container *ngFor="let avenant of Avenants" >
  <ng-container  *ngIf="avenant; else noAvenant">

  <div class="panel-body border border-purple rounded stl2">
    <table class="table table-hover">
      <thead class="thead-light" >
        <tr>
          <th>id</th>
         <th> Actions</th>
        </tr>
      </thead> 
      <tbody>
        <tr  *ngFor="let avenant of Avenants | orderBy: 'id' ">
          <td>{{avenant.id}}</td>
          <td>
            <button class="btn btn-danger" type="button" (click)="deleteAvenantId(avenant.id)"> </button>
          </td>
        </tr>
      </tbody>
    </table>
  </div>
 </ng-container>
<ng-template #noAvenant>
  <div>
   no avenant found !
  </div>
</ng-template>
</ng-container>

Using if-else directive

Reynier Rivero
  • 2,042
  • 2
  • 8
  • 12
0

You can do the following to your ngIf (if you have to check for objects) :

*ngIf="(avenant | json) != '{}'"

<ng-container *ngFor="let avenant of Avenants" >
   <ng-container *ngIf="(avenant | json) != '{}'"> //you could do like this

      <div class="panel-body border border-purple rounded stl2">
        <table class="table table-hover">
          <thead class="thead-light" >
            <tr>
              <th>id</th>
             <th> Actions</th>
            </tr>
          </thead> 
          <tbody>
            <tr  *ngFor="let avenant of Avenants | orderBy: 'id' ">
              <td>{{avenant.id}}</td>
              <td>
                <button class="btn btn-danger" type="button" (click)="deleteAvenantId(avenant.id)> </button>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
     </ng-container>
    <ng-container *ngIf="!avenant">
      <div>
       no avenant found !
      </div>
    </ng-container>
   </ng-container>

Hope this helps you!!

Suraj Gupta
  • 437
  • 8
  • 19
0

This worked :

 <ng-container *ngIf="(Avenants) == ''">
      <div>
        Pas d'avenants crées !
      </div>
Lamyae Lac
  • 43
  • 11