有时候我们在给页面显示给用户的信息需要一定处理格式化,这个时候我们就需要使用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>新闻热点
疑难解答