Home > Web Front-end > JS Tutorial > Detailed explanation of the use of $filter filter in AngularJS (custom filter)

Detailed explanation of the use of $filter filter in AngularJS (custom filter)

高洛峰
Release: 2017-02-07 13:51:47
Original
984 people have browsed it

1. Built-in filter

* $filter 过滤器,是angularJs中用来处理数据以更好的方式展示给我用户。比如格式化日期,转换大小写等等。
* 过滤器即有内置过滤器也支持自定义过滤器。内置过滤器很多,可以百度。关键是如何使用:
* 1.在HTML中直接使用内置过滤器
* 2.在js代码中使用内置过滤器
* 3.自定义过滤器
*
* (1)常用内置过滤器
*    number 数字过滤器,可以设置保留数字小数点后几位等
*    date  时间格式化过滤器,可自己设置时间格式
*    filter 过滤的数据一般是数组,数组中的数据可以是对象,字符串等
*    orderBy 排序根据数组中某一个元素的属性排序等
*    lowercase 转换小写
*    uppercase 转换大写
*    limitTo  字符串剪切 使用格式{{被剪切的字符串|limitTo:数值}} 数值绝对值代表要切得字符个数,正代表从头开始切,负值相反。
*
* */
Copy after login

2. Custom filter

/*
* 定义格式:
* 模块名称.filter(‘过滤器名字',function(){
*    return function(被过滤数据,条件1,条件2.。。。){
*    //过滤操作
*    }
* });
* */
Copy after login

Apply the above format to define two simple custom filters Define one filter with conditions and one without conditions.

(1) [Without conditions], function: fixed conversion (sometimes you will encounter role codes, store codes, etc. in the project, but when displayed, the corresponding Chinese must be displayed, such as field code: 101 represents Boss

At this time, there are more code values ​​like this, so it is better to use a filter)

myApp.filter("ChangeCode",function () {
  return function (inputData) {
    var changed = "";
    switch (inputData){
      case '101':changed = "老板";break;
      case '102':changed = "经理";break;
      case '103':changed = "员工";break;
    }
    return changed;
  }
});
/*完成,说一下使用场景(就这个过滤器的功能)和方式。
* 场景:服务器返回的数据中有个字段code,直接放标签里<div>{{data.code}}</div>,会显示code值而不是code值对应的职称,这时候就可以用这个专门
*    针对这个转换的自定义过滤器
* 使用方式:
*      (1)HTML中:<div>{{data.code | ChangeCode}}</div>//跟内置过滤器一样的方式
*      (2)js中:变量 = $filter("ChangeCode")(被过滤的code数据)//一样的调用方式
*     
* */
Copy after login

(2) [With conditions], the function filters out. A set of data whose field value is a certain value in an array. For example, here is a filter that filters out all ages that are a certain value. The parameter is age

myApp.filter("deleteByAge",function () {
  return function (input,byAge,age) {
    var array = [];
    for(var i=0;i<input.length;i++){
      if(input[i][byAge]!=age){
        array.push(input[i]);
      }
    }
    return array;
  }
})
/*
* 处理一组数据的时候一般很少用在HTML当中,带条件的自定义过滤器是根据年龄值,也可以根据数组元素中的任意一个属性值进行删除过滤。
* 使用方式:变量 = $filter("deleteByAge")(数组,“属性名称”,属性值);
* */
Copy after login

[Summary of how to use built-in filters]

(1) The general format in HTML is: {{Filtered data | Filter Name: Condition 1: Condition 2.. . . }} ; Filter conditions are separated by ':'.

(2) The general format in the code is: Variable = $filter("filter name") (filtered data, filter condition 1, Filter condition 2, . . . )

(1) Definition format: Module name

FilterName: Filter name

Parameter 1: Filtered data

Parameter 2: Generally filter conditions, there can be multiple, the following parameter 3 always It is all up to parameter N, add it as needed.

The above is the detailed explanation of the use of $filter filter (custom filter) in AngularJS introduced by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message. Editor Will reply to everyone promptly. I would also like to thank you all for your support of the PHP Chinese website!

For more detailed explanations on the use of $filter filters in AngularJS (custom filters), please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template