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

189
Visualizações
Ionic *ngIf - Check for global variables

Can *ngIf only check for local variables of the page or can I check global variables?

I want to do the following:

home.page.html

<div *ngIf="debugService.debugActive"></div>

debug.service.ts

debugActive: boolean = true;

contructor() {}
...

But right now, I have to first initialize the variable on my home.page.ts in order to use it in the html:

home.page.ts

import { DebugService } from 'src/app/services/debug.service';

localDebugActive: boolean = false;

constructor(
    private debugService: DebugService,
) {}

ngOnInit() {
    this.localDebugActive = this.debugService.debugActive;
}

home.page.html

<div *ngIf="localDebugActive"></div>

This means an extra-step and filling my whole pages with local variables. Is it possible without redeclaring it and using it directly in the HTML?

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

The only way a component gets access to a service is by injecting it, ensuring that dependency injection works correctly. Services aren't really global variables, as you put it: they're not simply "accessible from anywhere". You do need to use DI.

Technically you can inject a service as a public variable rather than a private one, and access its members directly from the template. This is generally considered code smell, however.

about 4 years ago · Juan Pablo Isaza Relatório

0

You can expose the injected service as public :

public debugService: DebugService

And use it in your template. Though I don't like that very much, but for simple display, that would do the trick.

about 4 years ago · Juan Pablo Isaza Relatório

0

There are two way:

  1. Make a service instance public

    constructor(
        public debugService: DebugService,
    ) {}
    
  2. Use a method (recommended way).

    public isDebugActive() {
        return this.debugService.debugActive;
    }
    
    <div *ngIf="isDebugActive()"></div>
    
about 4 years ago · Juan Pablo Isaza 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