Home >Web Front-end >JS Tutorial >How to use angularjs filter? Introduction to how to use angularjs filter

How to use angularjs filter? Introduction to how to use angularjs filter

寻∝梦
寻∝梦Original
2018-09-06 15:49:501559browse

This article mainly talks about the detailed explanation of how to use angularjs filters, and there are more formatting styles of angularjs filters. Next, let us read this article together.

Let’s first talk about the use of angularjs filters:

AnularJS filter is used to format the data that needs to be displayed to the user. There are many useful built-in filters, or you can write your own.

Call the filter through the | symbol within the template binding symbol {{ }} in HTML. For example, suppose we want to convert the string

to uppercase. We can convert each character in the string individually, or we can use the filter:

{{ name | uppercase }}

in JavaScript code Filters can be called via $filter. For example, when using a lowercase filter in JavaScript code:

app.controller('DemoController', ['$scope', '$filter',
function($scope, $filter) {
$scope.name = $filter('lowercase')('Ari');
}]);

When using a filter in the form of HTML, if you need to pass parameters to the filter, just add a colon

after the filter name. Can. If there are multiple parameters, you can add a colon after each parameter. For example, a numeric filter can limit the number of digits after the decimal point

. Write: 2 after the filter to pass 2 as a parameter to the filter:

<!-- 显示:123.46 -->
{{ 123.456789 | number:2 }}

1.currency

The currency filter can format a numerical value into currency format. Use {{ 123 | currency }} to convert

#123 into currency format.

The currency filter allows us to set the currency symbol ourselves. By default, the currency symbol of the region where the client is located will be used,

, but the currency symbol can also be customized. (If you want to see more, go to the AngularJS Learning Manual column on the PHP Chinese website)

2.date

date filter can change the date format into the required format. There are several date formats built into AngularJS. If no format is specified, the mediumDate format will be used by default. This format is shown in the example below.

The following are the built-in date formats that support localization:

    {{ today | date:'medium' }} 752d79d4a9c10733ea363575e3b5ad91
  • ##{{ today | date:'short' }} f4f46e8aa0a993461e8cd4c56ec815d5
  • {{ today | date:'fullDate' }} a3ddea33e934edb80917bc8b6f3e7f05
  • {{ today | date:'longDate' }} 1c356421dd8f925d33453d6fbb817e1d
  • {{ today | date:'mediumDate' } }c2d827205ae301b86c12768c59152195
  • {{ today | date:'shortDate' }} f879f611f80d89cbacd227ed41b66382
  • # #{{ today | date:'shortTime' }} 013d1656d9e50515528c03569797aa7d

  • ##Year Format

  • Four-digit year: {{ today | date:'yyyy' }} 2454eba55ce5af447d63d506f0b22009

Two-digit year: {{ today | date:'yy' }} b84312626dedf31a1067e9f84e4d6187One-digit year: {{ today | date:'y' }} 2454eba55ce5af447d63d506f0b22009

Month format

English month: {{ today | date:'MMMM' }} 7ee77e53246386e408232298689b6a6b

English month abbreviation: {{ today | date: 'MMM' }} 5812613cefcefb85ab7a2a7f2b19a771Number month: {{ today |date:'MM' }} 53031c7c3fbdb98da663f829dbcbec99

The month of the year: {{ today |date:'M' }} 0d07dfb83b43f33ffd3d813164ae49ba

Date formatting

Number date: {{ today|date:'dd' }} bdf347c31d338c5559ea24febd3d6d74

Day of the month: {{ today | date:'d' } } 10f30089400a4cdc8cfbd8d11fd537efEnglish week: {{ today | date:'EEEE' }} 7cc9ef7cc2267a79aa9f88d1a4d6dd20

English abbreviation of week :{{ today | date:'EEE' }} ea058d0751acaf64361b1abb55a9f5e4

Hour format

24-hour digital hour: { {today|date:'HH'}} ef72c658846c1084161df55b19ce8c24

Hour of the day: {{today|date:'H'}} a454f39fd7d5dc476cd8cacbac130c5512-hour digital hour: {{today|date:'hh'}} fb30a4b0d26192a15972624ccb337852

What is the morning or afternoon number? Hours: {{today|date:'h'}} fb30a4b0d26192a15972624ccb337852

Minute format

Numeric minutes: { { today | date:'mm' }} bdf347c31d338c5559ea24febd3d6d74

The minute of the hour: {{ today | date:'m' }} 10f30089400a4cdc8cfbd8d11fd537ef

Seconds format

Numeric seconds: {{ today | date:'ss' }} 29a28cf2f88ec30f0653887f8cf57c6f

The number of seconds in one minute: {{ today | date:'s' }} dfa9fc8a6a43443cb66797c35cf883e1The number of milliseconds: {{ today | date:'.sss' }} 132404d696749dd146bc462e6ef8004e

Here are some examples of custom date formats:

{{ today | date:&#39;MMMd, y&#39; }} <!-- Aug9, 2013 -->
{{ today | date:&#39;EEEE, d, M&#39; }} <!-- Thursday, 9, 8-->
{{ today | date:&#39;hh:mm:ss.sss&#39; }} <!-- 12:09:02.995 -->

filter The operator selects a subset from a given array and returns it as a new array.

例如,用下面的过滤器可以选择所有包含字母e的单词:

{{ [&#39;Ari&#39;,&#39;Lerner&#39;,&#39;Likes&#39;,&#39;To&#39;,&#39;Eat&#39;,&#39;Pizza&#39;] | filter:&#39;e&#39; }}
<!-- ["Lerner","Likes","Eat"] -->

如果要过滤对象,可以使用上面提到的对象过滤器。例如,如果有一个由people对象组成的

数组,每个对象都含有他们最喜欢吃的食物的列表,那么可以用下面的形式进行过滤:

{{ [{
&#39;name&#39;: &#39;Ari&#39;,
&#39;City&#39;: &#39;San Francisco&#39;,
&#39;favorite food&#39;: &#39;Pizza&#39;
},{
&#39;name&#39;: &#39;Nate&#39;,
&#39;City&#39;: &#39;San Francisco&#39;,
&#39;favorite food&#39;: &#39;indian food&#39;
}] | filter:{&#39;favorite food&#39;: &#39;Pizza&#39;} }}
<!-- [{"name":"Ari","City":"SanFrancisco","favoritefood":"Pizza"}] -->

也可以用自定义函数进行过滤(在这个例子中函数定义在$scope上):

{{ [&#39;Ari&#39;,&#39;likes&#39;,&#39;to&#39;,&#39;travel&#39;] | filter:isCapitalized }}
<!-- ["Ari"] -->

isCapitalized函数的功能是根据首字母是否为大写返回true或false,具体如下所示:

$scope.isCapitalized = function(str) {
return str[0] == str[0].toUpperCase();
};

自定义过滤器

首先,创建一个模块用以在应用中进行引用

angular.module(&#39;myApp.filters&#39;, [])
.filter(&#39;capitalize&#39;, function() {
return function(input) {
// input是我们传入的字符串
if (input) {
return input[0].toUpperCase() + input.slice(1);
}
});

现在,如果想将一个句子的首字母转换成大写形式,可以用过滤器先将整个句子都转换成小

写,再把首字母转换成大写:

<!-- Ginger loves dog treats -->
{{ &#39;ginger loves dog treats&#39; | lowercase | capitalize }}

以上就是AngularJS过滤器的使用方法(想看更多就到PHP中文网,AngularJS使用手册栏目学习),有问题的可以在下方提问。

【小编推荐】

angularjs如何搭建开发环境?angularjs搭建开发环境的过程分析

angularjs怎么开发web应用?angularjs开发web应用实例

The above is the detailed content of How to use angularjs filter? Introduction to how to use angularjs filter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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