AngularJS 过滤器完全匹配 [英] AngularJS Filter Exact Match

查看:43
本文介绍了AngularJS 过滤器完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要按类别过滤项目列表.我希望用户能够单击按钮,然后将过滤器应用于列表.

I need to filter a list of items by their category. I want the user to be able to click a button, and then have the filter applied to a list.

目前,我在一定程度上做到了这一点.

At the moment, I have this working to an extent.

假设例如我有一个电影列表,呈现如下:

Let's say for example I have a list of movies, rendered like so:

<li ng-repeat="movie in movieList | filter:filters">{{ movie.title }}</li>

而且我还有一个电影类型列表(呈现为按钮,点击后将过滤电影列表)呈现如下:

And I also have a list of movie genres (rendered as buttons, which when clicked will filter the list of movies) rendered like so:

<li ng-repeat="genre in genres">
    <a ng-click="filters.genre = genre.name" ng-click='changeGenre(genre.name)'>{{genre.name}}</a>
</li>

('changeGenre()' 函数所做的就是更新范围以显示当前正在查看的流派).

(All the 'changeGenre()' function does is update the scope to show which genre is currently being viewed).

现在这一切正常,直到我遇到一种情况,假设我有两种类型:动作"和动作冒险".当我按类型为动作"的电影进行过滤时,我不仅会得到动作片列表,还会得到动作冒险片列表.

Now this works fine up until I have a situation where, say I have the 2 genres: 'Action' and 'Action Adventure'. When I filter by movies with the genre 'Action', I not only get a list of Action movies, but also Action Adventure movies.

有没有办法使用过滤器获得精确匹配?

Is there a way I can get an exact match using the filter?

推荐答案

这现在在过滤器中本地提供.您可以将 true 传递给过滤器以启用严格匹配.

This is now provided natively within the filter. You can just pass true to the filter to enable strict matching.

<li ng-repeat="movie in movieList | filter : filters : true">{{ movie.title }}</li>

参考

https://docs.angularjs.org/api/ng/filter/filter

https://stackoverflow.com/a/18243147/1466430

这篇关于AngularJS 过滤器完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆