AngularJS Filter is nothing but a function returns a function used as an expression by pipe sign "|";
AngularJS provides some built-in filters such as
- currency
- lowercase
- orderBy
and many others but you can create your own, if you need!
Filters are simply takes an input and parameters, you process it and return the output.
How do you create your own custom filter?
Here is a simple example.
//.js
var app = angular.module("myApp",[]);
app.filter("truncateString",function( ){
return function(input, limit){
limit = parseInt(limit);
return (input.length > limit)?input.substring(0,limit):input;
}
});
// usage in html.
<span ng-bind="data.description | truncateString:'30'">,</span>
- truncateString filter will define limit to the text by parameter.
AngularJS provides some built-in filters such as
- currency
- lowercase
- orderBy
and many others but you can create your own, if you need!
Filters are simply takes an input and parameters, you process it and return the output.
How do you create your own custom filter?
Here is a simple example.
//.js
var app = angular.module("myApp",[]);
app.filter("truncateString",function( ){
return function(input, limit){
limit = parseInt(limit);
return (input.length > limit)?input.substring(0,limit):input;
}
});
// usage in html.
<span ng-bind="data.description | truncateString:'30'">,</span>
- truncateString filter will define limit to the text by parameter.
No comments:
Post a Comment