Part 13 Create a custom filter in AngularJS
Custom filter in AngularJS
1. Is a function that returns a function
2. Use the filter function to create a custom filter
Let us understand creating custom filter with an example.

Script.js : In the example below we are using the filter function to create a custom filter that converts integer values 1, 2, 3 to Male, Female and Not disclosed respectively.
gender is the name of the filter. With in the filter function we have
an anonymous function that returns another anonymous function. The input
parameter for the inner anonynous function is the gender integer value.
The switch statement in the function returns the corresponding string
value.
var app = angular
.module("myModule", [])
.filter("gender", function () {
return function (gender) {
switch (gender) {
case 1:
return "Male";
case 2:
return "Female";
case 3:
return "Not disclosed";
}
}
})
.controller("myController", function ($scope) { var employees = [
{ name: "Ben", gender: 1, salary: 55000 },
{ name: "Sara", gender: 2, salary: 68000 },
{ name: "Mark", gender: 1, salary: 57000 },
{ name: "Pam", gender: 2, salary: 53000 },
{ name: "Todd", gender: 3, salary: 60000 }
]; $scope.employees = employees;
});
HtmlPage1.html : In the view, we use the custom gender filter like any other angular built-in filter with a pipe character.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="Scripts/angular.min.js"></script>
<script src="Scripts/Script.js"></script>
<link href="Styles.css" rel="stylesheet" />
</head>
<body ng-app="myModule">
<div ng-controller="myController">
<table>
<thead>
<tr>
<th>Name</th>
<th>Gender</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="employee in employees">
<td> {{ employee.name }} </td>
<td> {{ employee.gender | gender}} </td>
<td> {{ employee.salary }} </td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
In the above example we have the custom filter in Script.js file. In a real world application you may the custom filters in a separate script file (Filters.js for example). To do this reference the module object and use the filter function.
Filter.js : The custom filter is moved to a separate file
/// <reference path="Script.js" />
app.filter("gender", function () {
return function (gender) {
switch (gender) {
case 1:
return "Male";
case 2:
return "Female";
case 3:
return "Not disclosed";
}
}
});
Script.js : After moving the filter function to a separate Filters.js file, the Script.js file will now look as shown below.
/// <reference path="angular.min.js" /> var app = angular
.module("myModule", [])
.controller("myController", function ($scope) { var employees = [
{ name: "Ben", gender: 1, salary: 55000 },
{ name: "Sara", gender: 2, salary: 68000 },
{ name: "Mark", gender: 1, salary: 57000 },
{ name: "Pam", gender: 2, salary: 53000 },
{ name: "Todd", gender: 3, salary: 60000 }
]; $scope.employees = employees;
});
HtmlPage1.html : The only change required in the view is to reference the Filters.js file
<script src="Scripts/Filters.js"></script>
Part 13 Create a custom filter in AngularJS的更多相关文章
- How to Create Custom Filters in AngularJs
http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction Filter ...
- [转]How to Create Custom Filters in AngularJs
本文转自:http://www.codeproject.com/Tips/829025/How-to-Create-Custom-Filters-in-AngularJs Introduction F ...
- Part 20 Create custom service in AngularJS
Whenever the case changes from lower to upper, a single space character should be inserted. This mea ...
- [转]How do you create a custom AuthorizeAttribute in ASP.NET Core?
问: I'm trying to make a custom authorization attribute in ASP.NET Core. In previous versions it was ...
- How could I create a custom windows message?
[问题] Our project is running on Windows CE 6.0 and is written in C++ . We have some problems with the ...
- [Angular] Create a custom validator for reactive forms in Angular
Also check: directive for form validation User input validation is a core part of creating proper HT ...
- [Angular] Create a custom validator for template driven forms in Angular
User input validation is a core part of creating proper HTML forms. Form validators not only help yo ...
- matlab中fspecial Create predefined 2-D filter以及中值滤波均值滤波以及高斯滤波
来源: 1.https://ww2.mathworks.cn/help/images/ref/fspecial.html?searchHighlight=fspecial&s_tid=doc_ ...
- how to create react custom hooks with arguments
how to create react custom hooks with arguments React Hooks & Custom Hooks // reusable custom ho ...
随机推荐
- RFID开发利器 proxmark3
Proxmark3 介绍 Proxmark3是由Jonathan Westhues设计并且开发的开源硬件,其主要用RFID的嗅探.读取以及克隆等的操作.其官方网站为:Jonathan Westhues ...
- QML学习笔记之三
import QtQuick 1.1 Row{ spacing:2 Rectangle{color:"red";width:50;height:50} Rectangle{colo ...
- Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones 水题
A. Case of the Zeros and Ones Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/con ...
- 【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式
背景: 年11月,微软宣布作为ECMA国际主要合作伙伴,将其开发的基于XML的文件格式标准化,称之为"Office Open XML" .Open XML的引进使office文档结 ...
- C++红旗之更短形式:500多字符且无法遵守原题规则
Purpose and Scope 研究五星红旗C++代码生成问题的代码压缩方法. 没有最短,仅仅有更短. 已经尽力了.爱因斯坦的三个小板凳里,我这是第四个. 继续深入压缩代码的方法肯定非常诡异了. ...
- [Express] Level 5: Route file
Using a Router Instance Let's refactor app.js to use a Router object. Create a new router object and ...
- iOS开发——UI篇Swift篇&玩转UItableView(二)高级功能
UItableView高级功能 class UITableViewControllerAF: UIViewController, UITableViewDataSource, UITableViewD ...
- SQL Server如何截断(Truncate)和收缩(Shrink)事务日志
当SQL Server截断事务日志时,它仅仅是在虚拟日志文件中做个标记,以便不再使用它,然后准备以重用形式来做备份(假如运载在完整或是批量日志恢复模型).也就是说,在使用简单恢复模型时,事务日志包括如 ...
- 文件系统缓存dirty_ratio与dirty_background_ratio两个参数区别
这两天在调优数据库性能的过程中需要降低操作系统文件Cache对数据库性能的影响,故调研了一些降低文件系统缓存大小的方法,其中一种是通过修改/proc/sys/vm/dirty_background_r ...
- Golang学习 - unicode/utf8 包
------------------------------------------------------------ // 编码所需的基本数字 const ( RuneError = '\uFFF ...