*ngIf does not function as expected, Angular

Asked

Viewed 317 times

-1

I basically have this *ngIf

<label>{{view._source.favoriteView }}</label>

       <!--Não é favorito-->
      <div *ngIf="view._source.favoriteView">
        <button (click)="unsetAsFavorite(view._id)" matTooltip="Favoritar" mat-icon-button >
          <mat-icon>favorite</mat-icon>
        </button>
      </div>

       <!--É favorito-->
      <div *ngIf="!view._source.favoriteView">
        <button (click)="setAsFavorite(view._id)" matTooltip="Desfavoritar" mat-icon-button color="warn" >
          <mat-icon>favorite</mat-icon>
        </button>
      </div>

The label displays the expected correct value (true / false), but is ALWAYS entering the first *ngIf, even if false, I tested using the *ngIf however available in the documentation and none worked, which I am missing?

1 answer

0


I had to use it this way:

<div *ngIf="view._source.favoriteView === 'false'">
            <button (click)="setAsFavorite(view._id)" matTooltip="Favoritar" mat-icon-button >
              <mat-icon>favorite</mat-icon>
            </button>
          </div>

           <!--É favorito-->
          <div *ngIf="view._source.favoriteView === 'true'">
            <button (click)="unsetAsFavorite(view._id)" matTooltip="Desfavoritar" mat-icon-button color="warn" >
              <mat-icon>favorite</mat-icon>
            </button>
          </div>

Because the values returned were of type 'String' and not Boolean

Browser other questions tagged

You are not signed in. Login or sign up in order to post.