Ellone
Ellone

Reputation: 3898

Add icon to angular material input

I am trying to add a search icon to my input search bar out of Angular Material :

<aside class="main-sidebar">
    <section class="sidebar control-sidebar-dark" id="leftMenu">
        <div>
            <!--  need to disable overflow-x somehow cuz of menu -->
            <md-tabs md-center-tabs id="menuTabs" md-dynamic-height="true" md-selected="selectedIndex">

                <md-tab label="<i class='fa fa-search'></i>" ng-if="handleResize()" search-Focus>
                    <md-content class="control-sidebar-dark" ng-style="{'height' : menuHeight}">
                        <!-- search Menu -->
                        <div>
                            <div>
                                <form action="javascript:void(0)" method="get" class="sidebar-form" id="searchForm">
                                    <div>
                                    <md-content md-theme="docs-dark">
                                        <md-input-container style="padding-bottom: 5px;">
                                            <label>{{isEnglish ? 'Search...' : 'Recherche...'}}</label>
                                            <input ng-model="searchInput" id="sInput" ng-change="filterCartoList(searchInput)" my-enter="filterCartoList(searchInput)">
                                        </md-input-container>
                                    </md-content>
                                    </div>

                                </form>
                                <div>

When I'm trying to reproduce the example with icons that we can see there : http://codepen.io/anon/pen/rOxMdR , I'm having style issues and nothing works properly.

Any idea how I could simply add a search icon to my existing input ?

Upvotes: 8

Views: 30054

Answers (2)

jsternadel
jsternadel

Reputation: 56

This question is very old but I just ran into the same issue this morning and the answer from @gaurav didn't work the same as what the OP was looking for in this link: https://material.angularjs.org/latest/#/demo/material.components.input

If you open the Chrome developer console you can inspect the element and see that the guys who wrote the demo code are using a custom class for the icon behavior of the email input in the bottom icons section. I essentially copied that behavior to achieve the same desired effect.

HTML:

<md-input-container class="md-block md-icon-left">
    <md-icon class="form-icon">lock_outline</md-icon>
    <label>Password</label>
    <input 
        type="password" ng-model="user.password" name="password" 
        ng-required="true" 
        ng-keyup="$event.keyCode == 13 && loginForm.$valid && login()"/>
</md-input-container>

CSS:

/* Angular extension */
md-input-container.md-input-invalid > md-icon.form-icon {
  color: #B71C1C;
}

One thing to note is that I had to add the class "md-icon-left" to my md-container or the icons would stack on top of the input. I am using angular material v1.1.0 and angular js v1.5.5 in my project. I hope this answers helps anyone else looking to achieve the same behavior as in the Angular Material demo.

Upvotes: 1

gaurav bhavsar
gaurav bhavsar

Reputation: 2043

As you are using angular-material-design and font-awesome-icon use <md-icon> as element with md-font-icon attribute.

And use class="md-icon-float" with md-inout-container.

Working plunker

Change this :

<md-content md-theme="docs-dark">
    <md-input-container style="padding-bottom: 5px;">
        <label>{{isEnglish ? 'Search...' : 'Recherche...'}}</label>
        <input ng-model="searchInput" id="sInput" ng-change="filterCartoList(searchInput)" my-enter="filterCartoList(searchInput)">
    </md-input-container>
</md-content>

To :

<md-content md-theme="docs-dark">
    <md-input-container class="md-icon-float">
        <label>{{isEnglish ? 'Search...' : 'Recherche...'}}</label>
        <md-icon md-font-icon="fa fa-search"></md-icon>
        <input ng-model="searchInput" id="sInput" ng-change="filterCartoList(searchInput)" my-enter="filterCartoList(searchInput)">
    </md-input-container>
</md-content>

Upvotes: 5

Related Questions