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

460
Visualizações
How can i use *ngIf in the host of my Directive?

I want to create a Directive that removes/adds itself to the DOM like *ngIf would do. What i have so far is:

@Directive({
    selector: '[myDirective]',
    host: {
        '[hidden]': 'hidden',
    }
})
export class MyDirective {
  constructor(private myService : MyService) {
  }

  hidden() {
    return !this.myService.valid;
  }

But what i need is something like:

@Directive({
    selector: '[myDirective]',
    host: {
        '*ngIf': 'hidden',
    }
})
...

Sadly i can not use '*ngIf': 'hidden' in host of Directive. The error i get is:

Can't bind to 'ngIf' since it isn't a known property of 'button'.

Also '[ngIf]': 'hidden' is not working.

So, how can i use ngIf in a Directive?

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

0

@Injectable()
export class MyService {
  valid = true;
}

@Directive({
  selector: '[myIf]'
})
export class MyIfDirective {
  constructor(private myService: MyService, 
        private el:ElementRef,
        private view:ViewContainerRef,
        private template:TemplateRef<any>) {
  }

  ngAfterViewInit(){
    if(this.myService.valid) {
      this.view.createEmbeddedView(this.template);
    }
  }
}

@Component({
  selector: 'my-app',
  template: `
    <div>
      Peek A Boo: <h2 *myIf>Hello</h2>
    </div>
  `,
})
export class App {
  constructor() {
  }
}
over 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