AngularJS自定义筛选器示例
时间:2020-02-23 14:29:34 来源:igfitidea点击:
在本文中,我们将介绍如何创建自己的过滤器。
过滤器用于按所需的方式格式化数据。
要创建过滤器,需要将其与模块一起附加。
angular.module('myApp')
.filter('customFilter', function () {
return function (input) {
if (input) {
return input;
}
};
});
举个例子,我们将字符串转换为大写,并用空格(“ ”)替换“_”。
例子:
复制下面的文字,打开记事本,粘贴并保存为angularJscustomfiltereExample.html文件然后在浏览器中打开它。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Angular js</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script>
<script>
var app = angular.module('myApp', []);
app.controller('theitroadContoller', function($scope) {
$scope.theitroadMsg = "Hello_from_theitroad";
}).filter('theitroadFilter',function(){
return function( str ) {
return str.toUpperCase().replace(/_/g,' ');
}
});
</script>
</head>
<body>
<div ng-app="myApp" ng-controller="theitroadContoller">
</br>
{{theitroadMsg|theitroadFilter}} !!!
</div>
</body>
</html>

