代码之家  ›  专栏  ›  技术社区  ›  Ralf de Kleine

哪个元素的重点是处理ng模糊

  •  0
  • Ralf de Kleine  · 技术社区  · 6 年前

    我想弄清楚在处理 ng-blur 事件我试过了 window.document.activeElement 但它总是“身体”。

    Html

    <body>
      <div name="element1" ng-blur="$ctrl.onBlur($event)">element1</div>
      <div name="element2" ng-blur="$ctrl.onBlur($event)">element2</div>
    </body>
    

    打字稿

    public onBlur($event: any): void {
       var value = $event.target.attributes["name"].value;
       // value is the name of the element where the event is triggered
       // but how to get the element to where the focus has gone to?
    }
    

    编辑

    使用 $timeout 服务

    public onBlur($event: any): void {
       this.$timeout(() => {
          if (this.$window.document.activeElement) {
             // do things
          }
       }, 1);
    }
    
    1 回复  |  直到 4 年前
        1
  •  0
  •   Ralf de Kleine    6 年前

    使用$timeout服务修复了它。

    public onBlur($event: any): void {
       this.$timeout(() => {
          if (this.$window.document.activeElement) {
             // do things
          }
       }, 1);
    }