Angular Material Dynamic Suffix Icon Color Change

Scenario

We want to change the search suffix icon color to primary when the #searchInput field is focused.

Approach

Use the focused property of the #searchInput field.

<mat-form-field floatLabel="never">
<input matInput
#searchInput="matInput"
type="text"
placeholder="Search"/>
<mat-icon [color]="searchInput.focused ? 'primary' : ''" matSuffix>search</mat-icon>
</mat-form-field>

Demo

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store