angular中的自定义过滤器
<!DOCTYPE HTML>
<html ng-app="myApp">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>无标题文档</title>
<script src="js/angular.min.js"></script>
</head>
<body>
<div ng-controller="Aaa">
<!-- {{name | firstUppercase}} -->
{{name}}
</div>
<div ng-controller="Bbb">
{{text | firstUppercase:4}}
</div>
<script>
var m1 = angular.module('myApp',[]);
//m1.run
//创建一个过滤器("过滤器的名字",回调函数)
m1.filter("firstUppercase",function(){
return function(data,number){
//原生js 字符串截取的操作
if(!number){
number = 1;
}
var str = data.substr(0,number).toUpperCase()+data.substr(number);
return str;
}
})
// $filter("firstUppercase")("要过滤的数据","过滤器接受的第一个参数","过滤器接受的第二个参数");
m1.controller('Aaa', ['$scope',"$filter" ,function($scope,$filter){
$scope.name= "kerwin";
$scope.name= $filter("firstUppercase")($scope.name,1)
}])
m1.controller('Bbb', ['$scope', function($scope){
$scope.text = "mytext";
}])
</script>
</body>
</html>