代码之家  ›  专栏  ›  技术社区  ›  tatsu

Angular 6如何正确使用focusout,但在单击自身时阻止其执行

  •  0
  • tatsu  · 技术社区  · 8 年前

    https://stackblitz.com/edit/angular-gitter-u7erfg?file=app%2Fapp.component.ts

    我的目标是让一个div的点击被捕获以阻止focusout执行(我不能阻止focusout触发,但是我可以在focusout调用的方法中放置if并检查我的条件,不管它是什么)。

    或者,从本质上说,有没有其他方法可以达到类似的效果,如果更好的话?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Poul Kruijt    8 年前

    这个怎么样: stackblitz

    export class AppComponent  {
      opened = false;
      dostuff = false;
    
      toggle(){
        this.opened = !this.opened
        if(!this.opened) this.dostuff = false;
      }
    
    
      mouseInside(event){
        this.dostuff = true;
        event.preventDefault();
      }
    
      focusOut(){
        if (!this.dostuff) {
          this.opened = false;
        }
      }
    
      onFileChange(event) {
        this.opened = false;
        this.dostuff = false;
      }
    }
    
    推荐文章