2

I have the following requirement,

I have an array as customers (values such as cust1,cust2,cust3). I have textbox, in which when I enter some values it will filter the customers arraylist and displays my result (For example : if i enter cust1 in textbox, it will display cust1) If the value entered in textbox not in the arraylist (customers), i want to change textbox colour to red. Could anyone guide to do this.

My approach

<div class="container">
<h2>View 1</h2>
Name:
<br />
<input type="text" ng-model="filter.name" /> -- I want to change this textbox colour
<br/>
<ul>
<li ng-repeat="cust in customers | filter : filter.name | orderBy:name">{{cust.name}} {{cust.city}}</li>
</ul>
<br/>
3
  • 3
    You have ng-model instead of ng-repeat <li ng-repeat="cust in customers | filter : filter.name | orderBy:name">{{cust.name}} {{cust.city}}</li> Commented Jun 10, 2015 at 8:02
  • Check this stackoverflow.com/questions/15316363/… , use ng-if to change the color Commented Jun 10, 2015 at 8:07
  • 1
    It is ng-repeat only, i have edited it. But the link provided was not discussing on color change of ng-model text box, instead it is displaying the filtered result alone. Commented Jun 10, 2015 at 9:24

1 Answer 1

1

The solution for this is rather simple.

First, you need to modify your ng-repeat in the following way:

ng-repeat="cust in customers | filter : filter.name | orderBy:name as filteredCustomers"

Notice the as filteredCustomers in the end. This saves the resulting filter to $scope.filteredCustomers.

Then, you just have to modify your input to use a conditional CSS class:

<input type="text" ng-model="filter.name" data-ng-class="{'red': filteredCustomers.length === 0}" />

Also you will have to define the .red CSS class with background-color: red; or something similar.

Proof of concept: http://jsbin.com/diyehagemo/2/

Sign up to request clarification or add additional context in comments.

2 Comments

Yes it is working, But When we are adding filteredCustomers it does not allow order by statement
The orderBy statement should also be working, but you have to put it before the as filteredCustomers. Have you edited the ng-repeat according to my answer? Also, you should not modify the filteredCustomers variable. This is just so that you can access the actual filtered AND sorted values in order to see it they are empty or not - for the purpose of colouring the input.

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.