Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

138
Vistas
Dependency injection in custom filters

I'm new to angular and trying create a custom filter which requires a service. I followed the answer here https://stackoverflow.com/a/43506252/15817005. It solved my issue partially.

Registering filter

angular.module('filters',[]).filter('dataFormat',['studentService', dataFormatFilter])

My Filter factory and filter function.

    export function dataFormatFilter(studentService){
console.log(studentService); // Having access here
return dataFormatFunction; 
}
 
function dataFormatFunction(name){
// All the formatting logic

//Need properties from studentService in this function.
}

I am able to access the properties from studentService in dataFormatFilter(factory function). Is there a way to get it in dataFormatFunction.

All the solutions i have seen use dataFormatFunction inside of factory itself dataFormatFilter. But i cannot follows this way.

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

What it looks like you need to to is return an arrow function that will keep the closure of your filter function so you can use the dependencies without having to pass them around. I wrote this plunkr to demonstrate.

The critical part being:

const dataFormatFilter = function (studentService) {
  return (name) => {
    return myReusableFunction(studentService, name);
  }
}

app.filter('dataFormat',['studentService', dataFormatFilter]);

Note that the returned value for the function is an arrow function. This isn't the only way to achieve what you are attempting, but should be the simplest.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda