先写个简单的例子,该过滤器是指定规定的字符串长度:
html:
<div ng-app="app" ng-controller="ctrl"> <p ng-repeat="text in texts> {{book | limitText:10}} </p></div>
js:
var app = angular.module("app",[]);app.filter('limitText', function() { return function(input, num) { if(input.length>num){ input = input.substring(0,num)+"..."; } return input; }; });app.controller("ctrl",["$scope",function($scope){ $scope.texts = ["大帅哥多发个梵蒂冈梵蒂冈","法规的非官","儿童热太热一天如图于一体"];}]);
显示
大帅哥多发个梵蒂冈梵...
法规的非官
儿童热太热一天如图于...
自定义过滤器可以接受参数(可多个),也可以不用参数。
但参数必须写在return 后面函数的第二个参数里,第一个参数是要处理的对象。
多个参数的用法
{{ expression | filter:argument1:argument2:... }}
转载于:https://www.cnblogs.com/Ushadow/p/5678469.html
相关资源:angularjs自定义过滤器demo示例