2017-01-02 27 views

回答

0

当使用多个指令,最好使用ATTR观察为u能碰上多个指令产生的问题隔离范围

app.directive('focusMe', function ($timeout) { 
    return { 
     link: function (scope, element, attr) { 
      attr.$observe('focusMe', function (value) { 
       if (value === "true") { 
        $timeout(function() { 
         element[0].focus(); 
        }); 
       } 
       else 
       { 
        $timeout(function() { 
         element[0].focusout(); 
        }); 
       } 
      }); 
     } 
    }; 
}); 

<input name="button1" focus-me="false" type="text"/> 
<input name="button2" focus-me="true" type="text"/> 
+0

这并不为我工作。第二个元素是关注的,但是即使焦点集合我设置为false,第一个元素也会立即关注。 – kmaci

+0

@kmaci我已更新我的答案。如果focusme为false,请在其他条件下设置焦点:) –

+0

对我而言效果相同。 – kmaci

相关问题