confirm.directive.ts
import { Directive,OnInit,HostListener,HostBinding,ElementRef} from '@angular/core'
@Directive({selector:'[confirms]'})
export class ConfirmsDirective implements OnInit{
public ele:any;
public val:any;
construtor(private el:ElementRef){
this.ele=this.el;
this.val=this.ele.nativeElement.attributes.confirms.nodeValue;
}
@HostBinding('style.background') background:string;
@HostBinding('style.transition') transition:string;
@HostListener('mousedown',['$event'])
onMousedown(){
this.background=this.val
this.transtion='0s'
}
@HostListener('mouseup',['$event'])
onMouseup(){
this.background=''
this.transtion='1s'
}
ngOnInit():void{
this.background="#0f0"
}
}
shared.module.ts
import {ConfirmsDirective} from './confirm/confirm.directive'
@ngModule({
declarations:[ConfirmsDirective],
exports:[ConfirmsDirective]
})
export class SharedModule{}
home.component.html
<button nz-button nzType="primary" confirms="red">确认</button>
home.module.ts
import {SharedModule} from'@shared/shared.module'
@ngModule({
imports:[SharedModule]
})
export class HomeModule{}