javascript - 在 friend 内部按friend.brand.id进行 Angular 过滤可以工作,但不能使用||当 NULL 时重置过滤器的未定义技巧

标签 javascript angularjs angular-filters

现在是 2075 年,不朽的人类奴役了凡人。有一家商店出售凡人 friend ,并正在尝试构建该应用程序,以便客户可以通过使用他们最喜欢的品牌过滤他们的 friend 来管理他们的 friend 。但显然 angular.js 已损坏。

<select ng-model="searchText2.id" ng-options="friend.brand.id as friend.name for friend in friends">
  <option value="">Select</option>
</select>

  <tr ng-repeat="friend in friends | filter:{brand:searchText2 || undefined}">

我在这里进行了很好的过滤,请不要误会我的意思,但是如果我单击选择时过滤器没有重置,用户体验管理器会将我提供给网络白化狮。

这是骗子 ---> http://plnkr.co/edit/1MPc0BqQGPNIJZ9FRR6z?p=preview

最佳答案

我已经更新了 plunker并提供一个可能的解决方案。

过滤器中,您可以使用三元语句来执行您想要的操作。

<tr ng-repeat="friend in friends | filter:{ brand:(searchText2.id === null) ? {id: undefined}: searchText2}">

当您选择“选择名称”的默认选项时,Angular 会将值设置为 null,但您希望传入 未定义 来清除过滤器。

希望对您的追求有所帮助!

关于javascript - 在 friend 内部按friend.brand.id进行 Angular 过滤可以工作,但不能使用||当 NULL 时重置过滤器的未定义技巧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32365044/

相关文章:

javascript - 过滤对象数组时出现无限摘要错误

javascript - AngularJS - 多维数组的自定义过滤器

javascript - 如何在 API 调用中使用 axios 在 React 中使用 async wait

javascript - 如何确保Jest bootstrap文件首先运行?

javascript - 如果我传递 "this"中的函数,监听器将不起作用

javascript - 谁能解释一下这段小代码吗?

javascript - Angularjs。在标签 &lt;script&gt; 中的 html 模板内发送变量

javascript - 如何检测 href 是否为图像?

javascript - 如何使用 Angular 将表单数据发送到服务器?

javascript - 按字母顺序排列的 Angular 过滤器