Angularjs 自动聚焦最新输入元素

Angularjs 自动聚焦最新输入元素,angularjs,directive,autofocus,Angularjs,Directive,Autofocus,我正在开发一个模块,在这个模块中,我需要手动创建一些文本输入(在输入或单击按钮时),并在输入添加到列表后立即自动聚焦该输入。到目前为止,该功能似乎正常工作,但当我打开控制台日志时,出现了$digest已在进行中的错误。有点奇怪,但如果我删除一些$eval或$apply,代码将无法工作 以下是我的plnk演示,供您参考: 我从这里开始跟踪自动对焦,即使应用了相同的逻辑,它也不会显示任何错误。唯一的区别是我用的是角度1.3,而他的是1.2 我应该如何改进代码以避免那些$digest错误?任何帮助都是

我正在开发一个模块,在这个模块中,我需要手动创建一些文本输入(在输入或单击按钮时),并在输入添加到列表后立即自动聚焦该输入。到目前为止,该功能似乎正常工作,但当我打开控制台日志时,出现了
$digest已在进行中的错误。有点奇怪,但如果我删除一些$eval或$apply,代码将无法工作

以下是我的plnk演示,供您参考:

我从这里开始跟踪自动对焦,即使应用了相同的逻辑,它也不会显示任何错误。唯一的区别是我用的是角度1.3,而他的是1.2

我应该如何改进代码以避免那些$digest错误?任何帮助都是非常感谢的,提前谢谢,所以它很有效

请看一看新指令:

  function customAutofocus($timeout) {
     return {
        restrict: 'A',
        link: function(scope, element, attrs) {
           //rember this gets run only only 
           //once just after creating the element, so I just neet to focus once, when
           // this digest cycle is done!
           $timeout(function() {
              // use a timout to foucus outside this digest cycle!
              element[0].focus(); //use focus function instead of autofocus attribute to avoid cross browser problem. And autofocus should only be used to mark an element to be focused when page loads.
           }, 0);
        }
     };
  }
这利用了角度的工作原理

  function customAutofocus($timeout) {
     return {
        restrict: 'A',
        link: function(scope, element, attrs) {
           //rember this gets run only only 
           //once just after creating the element, so I just neet to focus once, when
           // this digest cycle is done!
           $timeout(function() {
              // use a timout to foucus outside this digest cycle!
              element[0].focus(); //use focus function instead of autofocus attribute to avoid cross browser problem. And autofocus should only be used to mark an element to be focused when page loads.
           }, 0);
        }
     };
  }