Angular 2: set hover properties using ngStyle

user2182570 picture user2182570 · Feb 18, 2017 · Viewed 25.7k times · Source

I am trying to set the hover property states using [ngStyle]. The following only sets the normal state colors. When I mouse over the button, the button does not change to the pressed colors as expected.

<button (click)="logout()" 
                    class="btn  register-button"
                    [ngStyle]=" hover ? {'color': theme.logoutButtonColorPressed,
                                'border-color': theme.logoutButtonBorderColorPressed,
                                'background-color': theme.logoutButtonBackgroundColorPressed  } :

                               {'color': theme.logoutButtonColorNormal,
                                'border-color': theme.logoutButtonBorderColorNormal,
                                'background-color': theme.logoutButtonBackgroundColorNormal  }">Logout</button>

Answer

Max Koretskyi picture Max Koretskyi · Feb 19, 2017

If you want to switch styles on hover, add the following to the button

<button (mouseover)="hover=true" (mouseleave)="hover=false"...