2

I have an input field that is a filter for a long list. Next to the input field is a search icon. Basically when the user starts typing, I need to change the icon class.

I have my text field setup like so:

<input type="text" ng-change="change()" ng-model="query" />
<button class="btn"><i class="icon-search"></i></button>

Inside my controller I have defined:

$scope.change = function()
{
    //change the class of button based on the length of the input field     
}

Not really sure how I go about determining if there is input in the field using angular and changing the class. Am I taking the right approach here? Thanks

2 Answers 2

4

You can use ng-class on your ng-model

<i ng-class="{'icon-search': query.length}"></i>
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, to be more clear. The class is initially a search icon and I need the class to turn into icon-refresh. I think I figured it out though, its <i class="icon-search" ng-class="{'icon-refresh': query.length}"></i> thank you!
1

Just use ngClass. Your function $scope.change should return the class name, so

<button ng-class="change()"><i class="icon-search"></i></button>

and your controller function would look something like:

$scope.change = function() {
    if(something){
        return "classA";
    }
    else{
        return "classB";
    }
};

1 Comment

When would the change() method be invoked? Only the first time the UI is rendered?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.