【发布时间】:2017-04-26 11:06:22
【问题描述】:
我正在尝试在 Angular 4 中为 jQuery Confirm 创建一个指令。但是,我很难阻止绑定事件的发生。这是我的结构:
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;
});
}
}
确实出现了确认框,但是在它之前触发了事件,所以它没有用。我希望该指令停止触发事件,如果确认操作则触发它,否则取消它。
【问题讨论】:
-
为什么没用?
-
因为无论我确认与否,都会触发事件。
-
OMG Angular 4 发布了吗?在做什么 :O :O 角度 3 在哪里?
-
@RameshRajendran 他们跳过了 3。现在也只是“Angular”,当前的稳定版本是 v4.0.0。 6 个月后,将是 v5.0.0。
-
因为 this.router.navigate(['/menus']);总是在你初始化你的模式后被执行。您应该只在订阅结果中导航(点击 smt 后)
标签: angular angular-directive jquery-confirm