1

Well, I'm learning angular 2, and im starting by developing a log in form, and I'm having trouble making a button with conditional content.

This complement template's code would go like:

<button class="login-button" (click)="checkData()"> 
{{isLoading ? "loading...":"Log In" }} 
</button>

It checks the isLoading bool, and sets a inner html in condition to this bool value. Right?

Yes, this renders well, a button with a conditional content of "loading..." if isLoading is true, and "Log In" if its false.

But if want to add some google material icons inside the button it stops working:

<button class="login-button" (click)="checkData()"> 
{{isLoading ? "<i class='material-icons'>spinner</i>":"Log In <i class='material-icons'>arrow_forward</i>" }} 
</button>

It just doesn't work. It render a button with a content of "{{isLoading ? "spinner":"Log In arrow_forward" }}";

How can I add the html of the icons to my posible outcomes?

Kim Kern
  • 54,283
  • 17
  • 197
  • 195

2 Answers2

2

Try this -

<button class="login-button" [disabled]="isLoading" (click)="checkData()"> 
   <i [hidden]="!isLoading" class='material-icons'>spinner</i>
   <span [hidden]="isLoading">Log In <i class='material-icons'>arrow_forward</i></span>
</button>
Rehban Khatri
  • 924
  • 1
  • 7
  • 19
2

You can use ngSwitch

<button class="login-button" (click)="checkData()" [ngSwitch]="isLoading"> 
  <i class='material-icons' *ngSwitchCase="true">spinner</i>
  <span *ngSwitchCase="false">
    Log In <i class='material-icons'>arrow_forward</i>
  </span>
</button>
Lon Yang
  • 696
  • 5
  • 12