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

102
Visualizações
Angular 4 Confirm Directive

I'm trying to create a directive for jQuery Confirm in Angular 4. However, I'm having a hard time stopping binded events happening. Here is my structure:

menus.component.html:

<a (click)="delete(menu)" class="btn" confirm><i class="icon-trash"></i></a>

menus.component.ts:

delete(menu: Menu): void {
    this.menuService.delete(menu.id)
        .subscribe(
            error =>  this.errorMessage = <any>error);
}

confirm.directive.ts:

import {Directive, ElementRef, Input} from '@angular/core';

@Directive({ selector: '[confirm]' })
export class ConfirmDirective {
    constructor(el: ElementRef) {
        $(el.nativeElement).on('click', function () {
            $(this).confirm({
                confirm: function () {
                    return true;
                }
            });

            return false;
        });
    }
}

Confirmation box does appear, but the event is fired before it, so it is useless. I want this directive to stop an event from firing, fire it if the action is confirmed, cancel it otherwise.

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

0

I would work around it like this:

@Directive({ 
  selector: '[confirm]' 
})
export class ConfirmDirective {

  @Output('confirm-click') click: any = new EventEmitter();

  @HostListener('click', ['$event']) clicked(e) {
    $.confirm({
      buttons: {
        confirm: () => this.click.emit(),
        cancel: () => {}
      }
    });
  }

}

your html should look like:

<a (confirm-click)="delete(menu)" class="btn" confirm>Delete</a>

Plunker Example

over 4 years ago · Santiago Trujillo Relatório

0

You can actually combine directive name and output together.

 <button class="btn" (confirm)="delete()">delete</button>

 @Directive({
   selector: '[confirm]'
 }) 
 export class ConfirmDirective {
   @Output() confirm = new EventEmitter<any>();

   constructor(private el: ElementRef) {
   }

   @HostListener('click')
    onClick() {
      $.confirm({
      buttons: {
     confirm: () => this.confirm.emit()
   }
  });
 }
}
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