Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

138
Visualizações
Where to keep utility functions in angular2 that are available in templates

I have a few simple "pure" utility functions in my app which i want to keep in one place. The problem is, i can import them in my components by using:

import { utilities } from '../shared/utilities'

However these utility function/methods are not available in my template files (because they are bound to the component class only). I have to create an alias of these functions in my component in order to use them. Is there a better way to do this?

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I think you should be able to use the utilities in the template if you inject it in your controller. Something like this :

import { Utilities } from './shared/utils';

@Component({
    ....
    ....
})
export class ExampleComponent {
    constructor(public util: Utilities) { }
}

In your template you should be able to use util.your_function

Update:

export class ExampleComponent {
    constructor(public util: Utilities) { }

    get staticMethod1() { return Utilities.staticMethod1(); }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda