SoFunction
Updated on 2025-04-13

Mixed use of AngularJS custom service with fliter

In angular, Filter is used to format data. For example, in projects, there are many times when the data taken from the background is displayed directly in the book. We don’t understand its meaning. At this time, we need to format it ourselves and then display it on the interface. Traditional j requires some long strings of code and various allusions, and the filter provided by angular does need to be introduced a lot.

Let’s introduce the mixing of angularJS custom service and fliter. Let’s take a look.

1. Create a custom service "$swl"

var app = ('myApp', []); 
("$swl", function() { 
 = function(data) { 
return "("+data + " after,$swl"; 
}; 
 = function(data) { 
return "($swl,before " + data+")"; 
} 
})

2. Call custom service through controller

html code

<div ng-app="myApp" ng-controller="myCtrl"> 
{{name }} 
</div>

Controller code

("myCtrl", function($scope, $swl,$timeout) { 
$ = $("swl"); 
$timeout(function(){ 
$ = $("swl"); 
},2000) 
})

3. Mixed use with fliter

html code

<div ng-app="myApp" ng-controller="myCtrl"> 
{{name | before}} 
</div>

filter code

("before",["$swl",function($swl){ 
return function(data){ 
return $("(filter,"+data+")"); 
} 
}])

The above is the mixture of AngularJS custom service and fliter that the editor introduced to you. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support for my website!