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

2.2K
Views
Cómo cerrar primeng p-overlaypanel al hacer clic en un evento desde dentro de la superposición [Angular]

Estoy aprendiendo Angular. He creado un widget. Estoy usando el componente primeng , es decir, p-overlaypanel . El panel se abre cuando hago clic en un campo de entrada. Tengo dos botones Apply y Cancel dentro del propio panel superpuesto. Quiero que esta superposición se cierre cuando haga clic en el botón Cancelar. Aquí está el stackblitz . Y aquí está mi código.

selector de tiempo.componente.html

 <div class="timeselector"> <p-overlayPanel class="timeselector-overlay" #op> <div class="timeselector-panel"> <p>Time selector</p> <br /> <div> <button (click)="closeTimeselector($event, op)"> Cancel </button> <button> Apply </button> </div> </div> </p-overlayPanel> </div> <div (click)="op.toggle($event)"> <input type="text"> </div>

Y selector de tiempo.component.ts

 import { ... } from '@angular/core'; @Component({ ... }) export class TimeselectorComponent implements OnInit { timeselectorOpen = false; constructor() {} ngOnInit(): void {} closeTimeselector(event, element) { if (this.timeselectorOpen) { element.hide(event); this.timeselectorOpen = false; } console.log("closer called"); } }

Una de las posibles soluciones es utilizar:

 <button (click)="op.hide()"> Cancel </button>

Pero no puedo hacerlo porque tengo que hacer una tarea de limpieza y vaciar algunos valores cuando ese widget está cerrado.

Se llama a la función, que he comprobado en la consola. Pero la superposición no se está derrumbando. Puedes ir directamente al stackblitz. Por favor corrigeme.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Su problema es la condición "si" en la función "closeTimeselector". La variable "timeselectorOpen" nunca cambia, pero se verifica en la condición y siempre es "falsa".

Creo que no controlas el selector de tiempo Abrir

 import { ... } from '@angular/core'; @Component({ ... }) export class TimeselectorComponent implements OnInit { timeselectorOpen = false; constructor() {} ngOnInit(): void {} closeTimeselector(event, element) { element.hide(event); console.log("closer called"); } }

Por favor, prueba este código.

over 4 years ago · Santiago Trujillo Report

0

Usar Viewchild en la situación anterior podría ser lo mejor, ya que le brinda más control sobre las funciones

 @ViewChild('op', {static: false}) model;

(Según angular 8). En las versiones anteriores, la sintaxis era diferente, no necesita dar {static:false}.

 closeTimeselector(event) { this.model.hide(); if (this.timeselectorOpen) { this.timeselectorOpen = false; } console.log("closer called"); }

Al usar Viewchild, siempre tendrá una referencia para la plantilla, por lo que tendrá más control en su archivo TS.

over 4 years ago · Santiago Trujillo Report

0

Basado en la documentación

https://www.primefaces.org/primeng/#/overlaypanel

alternar, mostrar, ocultar son los métodos

Así que simplemente puede agregar

 <button (click)="op.hide()"> Cancel </button>

ejemplo de trabajo aquí:

https://stackblitz.com/edit/angular-wb4hgw

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!