[英]Use ng-repeat within ng-if AngularJS
I have the following requirement,我有以下要求,
I have an array as customers (values such as cust1,cust2,cust3
).我有一个数组作为客户(值如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.我有文本框,当我输入一些值时,它将过滤客户数组列表并显示我的结果(例如:如果我在文本框中输入cust1
,它将显示cust1
)如果在文本框中输入的值cust1
中(客户) ,我想将文本框颜色更改为红色。 Could anyone guide to do this.任何人都可以指导这样做。
<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/>
The solution for this is rather simple.对此的解决方案相当简单。
First, you need to modify your ng-repeat
in the following way:首先,您需要按以下方式修改您的ng-repeat
:
ng-repeat="cust in customers | filter : filter.name | orderBy:name as filteredCustomers"
Notice the as filteredCustomers
in the end.注意as filteredCustomers
到底。 This saves the resulting filter to $scope.filteredCustomers
.这会将结果过滤器保存到$scope.filteredCustomers
。
Then, you just have to modify your input to use a conditional CSS class:然后,您只需修改输入即可使用条件 CSS 类:
<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;
此外,您还必须使用background-color: red;
定义.red
CSS 类background-color: red;
or something similar.或类似的东西。
Proof of concept: http://jsbin.com/diyehagemo/2/概念证明: http : //jsbin.com/diyehagemo/2/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.