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

464
Vistas
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 Respuestas
Responde la pregunta

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 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