Assign a value to a variable in the template - Angular7

ShibinRagh picture ShibinRagh · May 22, 2019 · Viewed 12.2k times · Source

There are toggle two button (edit and submit), which button should work like toggle show/hide style on click

<button (click)="showEditBtn = false;" *ngIf="showEditBtn;"> Edit</button>
<button (click)="showEditBtn =  true;" *ngIf="!showEditBtn;">Submit</button> 

I need showEditBtn variable should be true in default without touching script file

Is it possible to assign a value to a variable in the template, like below example?

<div> {{  let showEditBtn = true  }}  </div>

stackblitz example

Answer

Dulanjaya Tennekoon picture Dulanjaya Tennekoon · May 22, 2019

Figured out. It is a bit of a hack. But works perfectly

<div *ngIf="true; let showEditBtn">
    <div> {{ showEditBtn }} </div>
    <button (click)="showEditBtn = false" *ngIf="showEditBtn"> Edit</button>
    <button (click)="showEditBtn = true" *ngIf="!showEditBtn">Submit</button>
</div>