Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put icon in placeholder in Angular Material?

I'm trying to put icon in placeholder. I tried this code:

<md-input name="name">
   <md-placeholder>
      <i class="material-icons app-input-icon">face</i> Name
   </md-placeholder>
</md-input>

It was working (was showing icon with placeholder) before I reinstalled angular material and updated the angular cli. For this code browser is giving this error now: "'md-input' is not a known element".

then I tried this code:

 <md-input-container>
    <input mdInput placeholder="Name" name="name2">
 </md-input-container>

It is working properly but How can I put 'face' icon in its placeholder ?

like image 837
AmanDeepSharma Avatar asked Apr 14 '17 18:04

AmanDeepSharma


People also ask

Does angular material have icons?

There are around 900+ material icons, all are from a single, small file(42KB) and divided into 10+ categories. We can use the file hosted in Google web font server or can be hosted in our own server.

How do I get an icon inside a input field?

To add icon inside the input element the <i> tag and <span> tag are used widely to add icons on the webpages. To add any icons on the webpages or in some specific area, it needs the fontawesome link inside the head tag. The fontawesome icon can be placed by using the fa prefix before the icon's name.

What is angular placeholder?

Placeholders can be used to enhance the experience of your application. You will, need set some custom widths to toggle their visibility. Their appearance, color, and sizing can be easily customized with our utility classes.


3 Answers

Your problem was not the md-placeholder tag. Just like the error said, it was md-input which was deprecated. Angular Material recently changed his md-input tag into a mdInput directive.

But the md-placeholder is still working (not sure if it will last though).

The following code should work then :

<md-input-container>
    <md-placeholder>
        <md-icon>face</md-icon> Name
    </md-placeholder>
    <input mdInput name="name">
</md-input-container>

An alternative is to use the mdPrefix or mdSuffix directives to your md-icon tag. That will display your icon on the left or right of your input, but it won't follow the placeholder when you click on it.

Example :

<md-input-container>
    <md-icon mdPrefix>face</md-icon>
    <input mdInput placeholder="Name" name="name">
</md-input-container>

Check the API reference for more informations.

like image 63
Grégory Gossart Avatar answered Oct 04 '22 02:10

Grégory Gossart


Angular 6

.example-full-width {
  width: 100%;
}
<mat-form-field  class="example-full-width">
 <mat-icon matPrefix>email</mat-icon>
  <input matInput
   type="email"
   tabindex="1"
   placeholder="Your Email"
   formControlName="email">
</mat-form-field>
like image 38
Whisher Avatar answered Oct 04 '22 04:10

Whisher


mat-suffix didn't work for me either. Keeping up with their docs is always a chore, but as of 5.1.1 this should work:

<mat-form-field class="example-form-field">
  <input matInput type="text" placeholder="Clearable input" [(ngModel)]="value"/>
  <button mat-button *ngIf="value" matSuffix mat-icon-button aria-label="Clear" (click)="value=''">
    <mat-icon>close</mat-icon>
  </button>
</mat-form-field>

Source: "Input with a clear button" example here: https://material.angular.io/components/input/examples

like image 21
cs_pupil Avatar answered Oct 04 '22 04:10

cs_pupil