Erbeyn Erbeyn - 3 months ago 14
AngularJS Question

Angularjs: Calling a method from service inside my filter

Inside my service.js, I included a filter that needs to call a function. However, the said function is declared just below my filter declaration (still inside my service.js). Below is a snippet of my filter:

angular
.module('app.inquiry')
.factory('InquiryService', InquiryService)
.filter("getCode", function(){
return function(item) {
searchProduct(item.name).then(function(result) {
item.reference.local = result;
return item.reference.local;
})
}
});


Inside my
function InquiryService
is another function named
searchProduct
, the function I used inside my filter. However, this doesnt work. Is it possible to call a method from my service inside a filter? If so, how can I reference the said method to be called?
Thank you!

Answer

Is it possible to call a method from my service inside a filter?

Yes, it's. You should inject your factory inside your filter, as below:

.filter("getCode", function(InquiryService) {

Then, you can call the method from your factory like this:

.filter("getCode", function(InquiryService) {
    return function(item) {
      InquiryService.searchProduct(item.name).then(function(result) {
        item.reference.local = result;
        return item.reference.local;
      })
    }
});
Comments