我在包含如下值的对象数组上使用 ng-repeat
指令:
{...,"Department": "ABC",...} // object 1
{...,"Department": "ABCDE",...} // object 2, etc
我只想列出包含部门“ABC”的项目。但是,当我这样做时
<table class="table" ng-controller="TestCtrl">
<tr>
<td>Name:</td>
<td>Department:</td>
</tr>
<tr ng-repeat="employee in employees | filter:{Department: 'ABC'}">
<td>{{employee.Email}}</td>
<td>{{employee.Department}}</td>
</tr>
</table>
我得到了部门字段中包含“ABC”的所有值的列表,包括那些以 ABC 开头的值,如下所示:“ABCDEF”。
我如何将结果限制为仅显示 ABC 部门的项目,而不显示任何其他可能碰巧以 ABC 作为前 3 个字符的部门?我尝试了 "limitTo",但这仅限制结果,而不限制字符。 Plnkr link
最佳答案
查看过滤器的比较器参数 ( https://docs.angularjs.org/api/ng/filter/filter )
解决方案是在过滤器中添加:true
:
employee in employees | filter:{Department: 'ABC'}:true
...用于“严格比较预期与实际”。
关于javascript - AngularJS:如何仅过滤具有特定 3 个字符值的项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32053958/