ebr*_*szl 2 events stoppropagation typescript angular2-directives angular
我用angular 2和PrimeNG构建了我的应用程序。我尝试使用按钮单击指令来检查用户权限。问题是;如果没有权限,请不要继续执行按钮单击操作。但是stopPropagation不会停止click事件。如果checkAuth()返回false,如何停止进程?
块引用
@Directive({
selector: '[checkAuthOnClick]'
})
export class CheckAuthorizationOnClickDirective {
user: Observable<User>;
@Input() allowedClaim: any;
observer: MutationObserver;
constructor(private element: ElementRef, private store: Store<fromRoot.State>) {
this.element = element.nativeElement;
}
@Output()
stop: EventEmitter<Event> = new EventEmitter;
@HostListener('click', ['$event, $event.target'])
onClick(event, targetElement) {
if (!this.checkAuth()) {
event.stopPropagation();
event.preventDefault();
event.cancelBuble = true;
event.stopImmediatePropagation();
this.stop.emit(event);
}
}
private checkAuth(): boolean {
this.user = this.store.select(fromRoot.currentUser);
if (this.user != undefined && this.allowedClaim != undefined) {
var hasClaim = false;
var description;
this.user.subscribe(x => {
if (Array.isArray(this.allowedClaim)) { //Gelen/Giden Faturalar? görüntüleme yetkileri tümü ve kendisine ait o.? birden çok oldu?u için app.routes'ta array olarak tan?mland?.
for (let i = 0; i < this.allowedClaim.length; i++) {
hasClaim = x.hasClaim(this.allowedClaim[i])
if (hasClaim)
break;
}
description = this.allowedClaim[0].Description;
}
else {
hasClaim = x.hasClaim(this.allowedClaim);
description = this.allowedClaim.Description;
}
});
if (hasClaim == false) {
var message = "Bu i?lem için yetkiniz yoktur.";
if (description != undefined) {
message = description + ' yetkiniz yoktur.'
}
this.store.dispatch(new ui.ToastMessagePushAction({ severity: 'warning', summary: message, detail: '' }));
}
}
return hasClaim;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样在html上的指令用法;
<button type="button" pButton icon="fa fa-file-code-o" (click)="createForm()" checkAuthOnClick [allowedClaim]="systemDefinedClaims?.CreateInvoiceDesign"></button>
Run Code Online (Sandbox Code Playgroud)
通过(click)和绑定事件HostBinding仅意味着将两个独立的事件绑定到目标元素,它们将被同时调用,并且不会互相影响,这意味着停止其中的任何一个都不会停止另一个事件。
您需要在指令的点击事件绑定中通过手动调用click事件(当前通过绑定(click))HostBinding。
// transfer click event into directive
@Input('clickEvent') clickEvent;
@HostListener('click', ['$event, $event.target'])
onClick(event, targetElement) {
if (!this.checkAuth()) {
event.stopPropagation();
event.preventDefault();
event.cancelBuble = true;
event.stopImmediatePropagation();
this.stop.emit(event);
} else {
this.clickEvent();
}
}
Run Code Online (Sandbox Code Playgroud)
请参阅示例演示。
| 归档时间: |
|
| 查看次数: |
1468 次 |
| 最近记录: |