addclass if search field has value: Angularjs

3k Views Asked by At

I have a search input field and a listbox,

    <input type="text"  class="inputSearch" placeholder="Search" ng-model="searchRoleOne"/>
    <i class="fa fa-search"></i>                                                         
    <select multiple class="listMutiple top5" ng-model="selectedRoleValue">
        <option ng-selected="selectrole" ng-repeat="role in roles | filter:searchRoleOne" value="{{role}}">{{role}}</option>      
    </select>

I need to addclass say

    .addClear{
        background: url(../images/Close.png) no-repeat right -10px center;
        background-repeat: no-repeat;
        background-position: right 5px center;
    }

whenever there is text, i should happen on change function. And on clicking the .addClear class search field should get empty and so does the ng-model.

How can i do this?

I tried using ng-class="{'addClear': searchRoleOne}" in search field. It isn't working properly.

2

There are 2 best solutions below

2
On BEST ANSWER

Simply you can try with this

Fiddle here

ng-class="{'addClear': searchRoleOne.length > 0}"

Hope this may help you

4
On

Try

ng-class="{addClear: searchRoleOne.length}"

This should work, see fiddle http://jsfiddle.net/HB7LU/5613/

what version of angular you using?