Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

465
Views
¿Cómo puedo usar *ngIf en el host de mi directiva?

Quiero crear una Directiva que se elimine/se agregue al DOM como lo haría *ngIf. Lo que tengo hasta ahora es:

 @Directive({ selector: '[myDirective]', host: { '[hidden]': 'hidden', } }) export class MyDirective { constructor(private myService : MyService) { } hidden() { return !this.myService.valid; }

Pero lo que necesito es algo como:

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

Lamentablemente, no puedo usar '*ngIf': 'hidden' en el host de la Directiva. El error que obtengo es:

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

También '[ngIf]': 'hidden' no funciona.

Entonces, ¿cómo puedo usar ngIf en una Directiva?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!