[英]Double click Angular directive
我在 Angular 7 中有以下模板:
<ul class="posts">
<li *ngFor="let post of posts">
<h2>{{post.title}}</h2>
<a (click)="delete(post)">Delete Post</a>
</li>
</ul>
我想創建一個確認指令用作:
<a (click)="delete(post)" confirm="Confirm delete" class="delete">Delete Post</a>
單擊的那一刻(一個就足夠了)它變為:
<a (click)="delete(post)" confirm="Confirm delete" class="delete confirm">Confirm delete</a>
那么會發生什么:
- 錨文本從“刪除帖子”變為里面的確認,例如“確認刪除”;
- 錨CSS類中添加了“confirm”類;
- 只有在“確認模式”點擊錨點后才會調用Delete(post)方法;
- 點擊“確認模式”或 5 秒后沒有被點擊,它會進入原始狀態:
<a (click)="delete(post)" confirm="Confirm delete" class="delete">Delete Post</a>
這可以通過指令完成嗎?
import { Directive } from '@angular/core';
@Directive({
selector: '[confirm]'
})
export class ConfirmDirective {
constructor(el: ElementRef) {
el.nativeElement ...
}
}
我開始創建指令,但我真的不知道如何做到這一點。
如果你真的想要,你實際上可以用指令來做。 嘗試這個:
import { Directive, ElementRef, Input, Renderer2, OnInit } from '@angular/core';
import { Observable, Subject, BehaviorSubject, timer, fromEvent } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Directive({
selector: '[confirm]'
})
export class ConfirmDirective implements OnInit {
@Input('confirm') delete: Function;
private confirm$ = fromEvent(this.el.nativeElement, 'click');
private confirmTimeout: number = 5000;
private timer$: Observable<number>;
private isConfirming = new BehaviorSubject<boolean>(false);
private isConfirming$ = this.isConfirming.asObservable();
constructor(private el: ElementRef, private renderer: Renderer2) {}
ngOnInit() {
this.isConfirming$.subscribe((isConfirming) => this.setLabel(isConfirming));
this.confirm$.subscribe((event: any) => this.doConfirm());
}
setLabel(isConfirming: boolean): void {
// set the correct element text and styles
let text: any;
let textEl = this.renderer.createElement('span');
if (this.el.nativeElement.firstChild) {
this.renderer.removeChild(this.el.nativeElement, this.el.nativeElement.firstChild);
}
if (this.isConfirming.value) { // we are confirming right now
text = this.renderer.createText('Please confirm delete');
this.renderer.addClass(this.el.nativeElement, 'delete');
} else {
text = this.renderer.createText('Delete');
this.renderer.removeClass(this.el.nativeElement, 'delete');
}
this.renderer.appendChild(this.el.nativeElement, text);
}
doConfirm(): void {
if (this.isConfirming.value === false) { // start confirming
this.timer$ = timer(this.confirmTimeout);
this.isConfirming.next(true);
// start the timer
this.timer$
.pipe(
takeUntil(this.confirm$) // stop timer when confirm$ emits (this happens when the button is clicked again)
)
.subscribe(() => {
this.isConfirming.next(false); // timeout done - confirm cancelled
});
} else { // delete confirmation
this.isConfirming.next(false);
this.delete(); // this is the delete action that was passed to the directive
}
}
}
您可以將它應用於這樣的元素,將實際的delete
方法作為參數傳遞。
<button type="button" [confirm]="delete"></button>
工作示例: https : //stackblitz.com/edit/angular-wdfcux
不確定指令是最好的方法。 它可能可以完成,但您必須以某種方式攔截點擊處理程序和/或將刪除方法傳遞給它。 估計會很亂。
我可能會為刪除按鈕創建一個組件並在那里處理它(實際上這是一個謊言,如果是我,我會使用本機confirm
對話框並完成它,但您不想這樣做)。
像這樣的東西:
import { Component } from '@angular/core';
import { Observable, Subject, timer } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
@Component({
selector: 'delete-button',
template: `<button type="button" (click)="delete()" [ngClass]="{ delete: isConfirming }">{{ label }}</button>`,
styles: ['.delete { background-color: teal; color: white; } ']
})
export class DeleteButtonComponent {
private confirmTimeout: number = 5000;
private timer$: Observable<number>;
private cancelTimer = new Subject();
public isConfirming: boolean = false;
constructor() {}
get label(): string {
return this.isConfirming
? 'Please confirm delete'
: 'Delete'
}
delete() {
if (!this.isConfirming) {
this.timer$ = timer(this.confirmTimeout);
this.isConfirming = true;
this.timer$
.pipe(
takeUntil(this.cancelTimer)
).subscribe(() => {
this.isConfirming = false;
}, null, () => this.isConfirming = false);
} else {
this.cancelTimer.next();
// really delete
}
}
}
你可以使用 rxjs takeUntil,
posts: any[] = [
{ id: 1, title: 'post 1', deleteText: 'Delete Post' },
{ id: 2, title: 'post 2', deleteText: 'Delete Post' }
];
delete(post) {
post.deleteText = 'Click to Confirm';
post.css = 'custom';
let confirm$ = fromEvent(document.getElementById('post-' + post.id), 'click');
let timer$ = timer(5000)
confirm$
.pipe(
takeUntil(timer$)
)
.subscribe(() => {
console.log('ready to delete');
this.posts = this.posts.filter(p => p.id !== post.id);
});
timer$
.subscribe(() => {
if (this.posts.find(p => p.id === post.id)) {
console.log('timer is up, abort delete');
post.deleteText = 'Delete Post';
post.css = '';
}
});
}
html:
<ul class="posts">
<li *ngFor="let post of posts">
<h2>{{post.title}}</h2>
<a (click)="delete(post)" [ngClass]="post.css" [id]="'post-'+post.id"> {{post.deleteText}}</a>
</li>
</ul>
演示: https : //stackblitz.com/edit/angular-7-master-xg8phb
(還需要管理訂閱)
您可以通過帶有@HostBinding
和@HostListener
的指令以及綁定到指令本身的任意@Input
來添加宿主元素行為綁定和偵聽器:
@Directive({
selector: '[confirm]'
})
export class ConfirmDirective {
@HostListener('dblclick') onDoubleClick(event) {
// .. do double click logic, just like binding (dbclick) to an element
}
@HostBinding('class.confirm') confirmStyle: boolean; // toggles class on host, just like with template binding
@Input('confirm') confirm: boolean; // State from outside the directive that can be bound to the directive attribute directly, i.e. 'click to confirm' box
constructor(el: ElementRef) {
el.nativeElement ...
}
}
如果您希望使用確認按鈕的指令直接添加 HTML/標記,則最好將此行為與組件一起包裝。 組件用於視圖; 指令是針對行為的。 一種想法是將確認對話框(?)包裝到指令可以調用的服務中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.