首页 > 网站 > WEB开发 > 正文

angular之过滤器

2024-04-27 15:03:39
字体:
来源:转载
供稿:网友

一、前言

有时候我们在给页面显示给用户的信息需要一定处理格式化,这个时候我们就需要使用angular的filter来处理,angular默认给我们提供了很多内置的过滤器

二、内置过滤器

1、currency 作用:用来美元转换的。 使用:

<h1> {{1000 | currency}} </h1>

2、date 作用:格式化时间的。 使用:

<h1> {{1000 | date : 'yyyy-MM-dd hh:mm:ss' }} </h1>

3、json 作用:格式化json字符串的。 使用:

<div ng-controller="DemoController"> <PRe> {{p1 | json:2}} </pre></div><script> angular.module('app', []) .controller('DemoController', ['$scope', '$filter', function ($scope, $filter) { $scope.p1 = { name: '张三', age: 19, gender: false, zhengfang: { name: '张三', age: 19, gender: false }, ceshi: [{ name: '张三', age: 19, gender: false }, { name: '张三', age: 19, gender: false }, { name: '张三', age: 19, gender: false }] }; }])</script>

还有filter、limitTo、number等内置过滤器。

三、自定义过滤器

调用module的filter方法

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Title</title> <script src="../../lib/angular/angular.js"></script></head><body ng-app="app"><div> <h1>{{false | checkData }}</h1></div><script> angular.module('app', []) .filter('checkData', function () { return function (input, style) { style = style || 1; switch (style) { case 1: return input ? '/u2713' : '/u2718'; break; case 2: return input ? '/u2714' : '/u2719'; break; } } })</script></body></html>
上一篇:Ui-Router

下一篇:spring boot 整合mybatis

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表