search
HomeWeb Front-endJS TutorialHow to use angularjs filter? Introduction to how to use angularjs filter

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' }}
  • ##{{ today | date:'short' }}
  • {{ today | date:'fullDate' }}
  • {{ today | date:'longDate' }}
  • {{ today | date:'mediumDate' } }
  • {{ today | date:'shortDate' }}
  • # #{{ today | date:'shortTime' }}

  • ##Year Format

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

Two-digit year: {{ today | date:'yy' }} One-digit year: {{ today | date:'y' }}

Month format

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

English month abbreviation: {{ today | date: 'MMM' }} Number month: {{ today |date:'MM' }}

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

Date formatting

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

Day of the month: {{ today | date:'d' } } English week: {{ today | date:'EEEE' }}

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

Hour format

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

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

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

Minute format

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

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

Seconds format

Numeric seconds: {{ today | date:'ss' }} The number of milliseconds: {{ today | date:'.sss' }}

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
Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

JavaScript Frameworks: Powering Modern Web DevelopmentJavaScript Frameworks: Powering Modern Web DevelopmentMay 02, 2025 am 12:04 AM

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

The Relationship Between JavaScript, C  , and BrowsersThe Relationship Between JavaScript, C , and BrowsersMay 01, 2025 am 12:06 AM

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr

Node.js Streams with TypeScriptNode.js Streams with TypeScriptApr 30, 2025 am 08:22 AM

Node.js excels at efficient I/O, largely thanks to streams. Streams process data incrementally, avoiding memory overload—ideal for large files, network tasks, and real-time applications. Combining streams with TypeScript's type safety creates a powe

Python vs. JavaScript: Performance and Efficiency ConsiderationsPython vs. JavaScript: Performance and Efficiency ConsiderationsApr 30, 2025 am 12:08 AM

The differences in performance and efficiency between Python and JavaScript are mainly reflected in: 1) As an interpreted language, Python runs slowly but has high development efficiency and is suitable for rapid prototype development; 2) JavaScript is limited to single thread in the browser, but multi-threading and asynchronous I/O can be used to improve performance in Node.js, and both have advantages in actual projects.

The Origins of JavaScript: Exploring Its Implementation LanguageThe Origins of JavaScript: Exploring Its Implementation LanguageApr 29, 2025 am 12:51 AM

JavaScript originated in 1995 and was created by Brandon Ike, and realized the language into C. 1.C language provides high performance and system-level programming capabilities for JavaScript. 2. JavaScript's memory management and performance optimization rely on C language. 3. The cross-platform feature of C language helps JavaScript run efficiently on different operating systems.

Behind the Scenes: What Language Powers JavaScript?Behind the Scenes: What Language Powers JavaScript?Apr 28, 2025 am 12:01 AM

JavaScript runs in browsers and Node.js environments and relies on the JavaScript engine to parse and execute code. 1) Generate abstract syntax tree (AST) in the parsing stage; 2) convert AST into bytecode or machine code in the compilation stage; 3) execute the compiled code in the execution stage.

The Future of Python and JavaScript: Trends and PredictionsThe Future of Python and JavaScript: Trends and PredictionsApr 27, 2025 am 12:21 AM

The future trends of Python and JavaScript include: 1. Python will consolidate its position in the fields of scientific computing and AI, 2. JavaScript will promote the development of web technology, 3. Cross-platform development will become a hot topic, and 4. Performance optimization will be the focus. Both will continue to expand application scenarios in their respective fields and make more breakthroughs in performance.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.