angularJs中筛选功能-angular.filter-1

技术分享:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/angular-filter-learn-1/

以下介绍为自己在使用angular-filter时,简单总结的用法。

开始

1.你可以使用4中不同的方法来安装angular-filter:

  • 克隆或创建这个存储库
  • 通过bower:通过在你的终端执行:$ bower install angular-filter
  • 通过npm:通过在你的终端执行:$ npm install angular-filter
  • 通过cdnjs:http://www.cdnjs.com/libraries/angular-filter

2.在包含或Angular本身外,在引用angular-filter.js(或者angular-filter.min.js)在你的项目index.html中;

3.添加‘angular.filter’依赖项;

当你做完这些,你的文件看起来就类似以下:

<!doctype html>
<html ng-app="myApp">
<head>

</head>
<body>
    ...    <script src="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.0/angular.min.js"></script>
    <script src="bower_components/angular-filter/dist/angular-filter.min.js"></script>
    ...    <script>        var myApp = angular.module(‘myApp‘, [‘angular.filter‘]);    </script>
    ...</body>
</html>

filter

从数组中选取一个子集,并将其返回成一个新的数组;

用法:

// html中:{{ collection | filter : expression : comparator}}// js中:$filter(‘filter‘)(array, expression, comparator)参数:array:想筛选的数组
     expression:用于从数组中筛选的条件
     comparator:用于确定预期值(从筛选器表达式)和实际值(从数组中的对象)中使用的比较器,应视为匹配。
 
$scope.friends = [
{name:‘John‘, phone:‘555-1276‘},
{name:‘Mary‘, phone:‘800-BIG-MARY‘},
{name:‘Mike‘, phone:‘555-4321‘}
]<tr ng-repeat="friendObj in friends | filter:{name:‘John‘}">
    <td>{{friendObj.name}}</td>
    <td>{{friendObj.phone}}</td>
</tr>
 
<--result
John 555-1276-->

Date

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

将日期筛选为想要的日期格式;

用法:

// html中:{{ collection | date : format : timezone}}// js中:$filter(‘date‘)(date, format, timezone)参数:format为设置的日期格式,例如:‘yyyy-MM-dd‘
     timezone被用于格式化时区。 
<span>{{1288323623006 | date:‘yyyy-MM-dd HH:mm:ss Z‘}}</span>
<--result:2010-10-29 11:40:23 +0800-->

Collection

concat

将另外一个对象或者数组拼接到已有的对象或者数组之后;

function MainController ($scope) {
  $scope.array = [ {a: 1}, {a: 2} ];
  $scope.object = {    0: {a: 3},    1: {a: 4}
  };
}<li ng-repeat="elm in array | concat:object">
  {{ elm.a }}</li>

<!--result:1 2 3 4-->

unique

从数组或者对象中移除重复项;

如果提供的是一个字符串,它将用提供的表达式来过滤掉重复使用的。

用法:

//html中:{{collection | unique: ‘property‘}};//js中:$filter(‘unique‘)(collection, ‘property‘); 参数:collection被筛选的数组或者对象
      property去掉这个属性中的重复值
function MainController ($scope) {
  $scope.orders = [
    { id:1, customer: { name: ‘John‘, id: 10 } },
    { id:2, customer: { name: ‘William‘, id: 20 } },
    { id:3, customer: { name: ‘John‘, id: 10 } },
    { id:4, customer: { name: ‘William‘, id: 20 } },
    { id:5, customer: { name: ‘Clive‘, id: 30 } }
  ];
}<tr ng-repeat="order in orders | unique: ‘customer.id‘" >
   <td> {{ order.customer.name }} , {{ order.customer.id }} </td>
</tr>
<!-- result:
All customers list:
John 10William 20Clive 30-->

join

将一个数组转换为一个字符串;

默认情况下,它将加入元素与一个单一的空间,但是你可以提供自己的分隔符。

用法:

//html中:{{collection | join: ‘,‘}};//js中:collection.join(‘,‘); 参数:collection 需要转换的数组,得到用,连接的字符串
 
$scope.names = [‘John‘, ‘Sebastian‘, ‘Will‘, ‘James‘];<p>{{ names | join:‘,‘ }}</p>
<!--result:
John, Sebastian, Will, James--> 
 ps:js中的split()函数可以使用一个字符串中的某一分隔符将其字符串分隔成为数组。

Math

number

用来精确浮点数;

用法:

//html中:{{ number_expression | number : fractionSize}}//js中:$filter(‘number‘)(number, fractionSize)参数:number为待精确的数字
     fractionSize(可选)为小数点后精确位数,默认值是3
 
function MainController ($scope) {
  $scope.val=1234.56789;
}<li>{{val | number:2}}</li>
<li>{{val | number}}</li>
 
<-- result:1234.561234.567-->

max/min

用来查找并返回数组中的最大值(最小值);

用法:

html中:{{array|max:optional}}
js中:$filter(‘max‘)(array,‘optional‘) 
<p> {{ [1,2,3,4,7,8,9] | max }}</p>
<--result9-->

sum

计算数组中的值;

用法:

html中:{{array|sum:optional}}
js中:$filter(‘sum‘)(array,‘optional‘)
 
{{[1,2,3] | sum:10}}<--result16-->

GitHub英文文档:https://github.com/a8m/angular-filter

时间: 2024-08-25 01:33:40

angularJs中筛选功能-angular.filter-1的相关文章

AngularJS中的过滤器(filter)

AngularJS中的过滤器是用于对数据的格式化,或者筛选的函数,可以直接通过以下语法使用: {{expression|filter}} {{expression|filter1|filter2}} {{expression|filter1:param1,param2,...|filter2} 过滤器的种类有number,currency,date,json,limitTo,lowercase,uppercase,filter,orderBy. <!DOCTYPE html> <html

【经验】angularjs 实现带查找筛选功能的select下拉框

一.背景 对于select的下拉列表,像国家选择这样的功能,全世界那么多国家,一直拉滚动条多辛苦,眼睛也要盯着找,累!so,为优化用户体验,带查找功能的下拉框是非常非常有必要的.都知道jquery里有这样的插件,但我们用的是Angularjs,更希望用双向绑定,指令的方式优雅地解决这个问题. 分析:    目标 在原来的<select ng-options="">标签上新加一个属性 select-search 就能支持查找的功能.如果这个属性没起作用,也不影响原来的sele

关于JS中数组splice,concat的用法和AngularJs中filter的补充知识

一 JS数组相关操作 1. splice函数,可做插入,删除,替换操作 1 <script> 2 "use strict" 3 var arr = ['z3']; 4 arr.splice(1,0,'l4'); 5 alert(arr); //z3,l4 6 arr.splice(1,1,'w5'); 7 alert(arr); //z3,w5 8 arr.splice(1,1); 9 alert(arr); //z3 10 arr.splice(10,1,'l4','w5

angularjs中的filter(过滤器)

源文地址:http://www.ncloud.hk/技术分享/angularjs中的filter-过滤器/ filters可以用来格式化数据.例如把时间格式化成为yyyy-MM-dd的形式来呈现给用户,方便查看.angularjs内置了一些filters,我们也可以根据自己的需要自定义一些filters. 下面是angularjs提供的filters: currency:格式化一个数字成为一种货币.当没有提供任何货币符号时,使用当前区域的默认符号. number:格式化数字成为文本 filter

(译) 在AngularJS中使用的表单验证功能【转】

验证功能是AngularJS里面最酷炫的功能之一,它可以让你写出一个具有良好用户体验的Web应用. 在AngularJS中,有许多用于验证的指令.我们将先学习几个最流行的内置指令,然后再创建一个自定义验证规则的指令. <form name="form"> <label name="email">Your email</label> <input type="email" name="email&

iwebshop框架中后台筛选功能的思路整理

本人在实际开发中,根据产品经理需求,在iwebshop框架原有的后台订单筛选功能里增加商户名称模糊筛选! 开发思路:首先要明白筛选的数据在数据库中的位置,对要筛选的字段进行模糊查询! 开发主要注意:因为iwebshop后台订单筛选功能是写在封装好的一个类下面 order/order_class.php里面,所以sql查询后的遍历和sql本身的条件,一定要注意书写正确! 实际开发书写代码思路讲解: if(isset($search['name']) && isset($search['key

善用Linux与Windows中的筛选功能及其他有用功能

cmd中的检索目录结构是用 tree命令,检索本目录中这一级别的所有文件是dir,要是文件很多时需要用到检索功能 dir | find "abc" #####主要find之后要加双引号 netstat -ano | find "1521" #####这里可以方便查找监听此端口号的网络连接啥的 在Linux中检索当前目录使用ll或者ls,检索进程使用ps,这里可以使用筛选功能grep 主要之后是不用双引号的 ps -ef | grep weblogic ###查找we

淡淡理解下AngularJS中的module

在AngularJS中module是一个核心的存在,包括了很多方面,比如controller, config, service, factory, directive, constant, 等等. 在Javascript中如何实现类似module的功能呢? 或者说,我们定义一个函数,如何把函数内的函数向外界开放呢? 我想,可以把函数中的函数作为一个对象的键值,从而向外界开放. 这样说很笼统,其实是这样的: var myModule = function outerFuction(){ var m

AngularJS学习--- AngularJS中数据双向绑定(two-way data-binding) orderBy step4

1.切换工作目录 git checkout step-4 #切换分支,切换到第4步 npm start #启动项目 2.代码 app/index.html Search: <input ng-model="query"> Sort by: <select ng-model="orderProp"> <option value="name">Alphabetical</option> <opti