3 回答

TA貢獻2021條經驗 獲得超8個贊
創建一個函數,例如 MouseHandlerEv,您可以在其中接收布爾值:
.HTML文件
<div (mouseover)="mouseEvHandler(true)" (mouseout)="mouseEvHandler(false)"></div>
.TS文件
mouseEvHandler(status){
status ? FunctionTrue() : FunctionFalse();
}
例子:
function mouseEvHandler(status){
status ? sayHi() : sayBye();
}
function sayHi() {
console.log('HI');
}
function sayBye() {
console.log('Bye');
}
<div onmouseover="mouseEvHandler(true)" onmouseout="mouseEvHandler(false)">MESSAGE ON CONSOLE</div>
將其外推到角度

TA貢獻1812條經驗 獲得超5個贊
ngOnInit()目前,由于您正在檢查鉤子中的值,因此僅在組件開始時檢查一次值。您可以改為嘗試使用 RxJS 使其響應fromEvent并使用它來觸發事件。
嘗試以下
模板
<div #mouseControl></div>
import { Component, ViewChild, ElementRef, AfterViewInit } from '@angular/core';
@Component({ ... })
export class AppComponent implements AfterViewInit {
@ViewChild('mouseControl') mouseControl: ElementRef<any>;
ngAfterViewInit() {
fromEvent(this.mouseControl.nativeElement, 'mouseover').subscribe(
_ => otherFunc();
);
fromEvent(this.mouseControl.nativeElement, 'mouseout').subscribe(
_ => func();
);
}
}

TA貢獻1998條經驗 獲得超6個贊
您可以像下面這樣簡單地使用它而無需為事件創建函數:
<div (mouseover)="mouseEv=true" (mouseout)="mouseEv=false"></div>
您還可以嘗試將 true 和 false 作為方法參數直接傳遞給 mouseOver(true) 并在組件中接收其值。
添加回答
舉報