我正在嘗試在Angular 4中爲jQuery Confirm創建一個指令。但是,我很難停止綁定的事件發生。這裏是我的結構:Angular 4確認指令
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;
});
}
}
確認框確實出現,但事件在它之前被觸發,所以它沒用。我希望這個指令能夠阻止一個事件的發生,如果這個動作被確認了就會觸發它,否則就取消它。
爲什麼沒用? –
因爲無論我確認與否,事件總是被觸發。 – yenerunver
OMG Angular 4發佈了嗎?什麼在做:O:O角3在哪裏? –