如何从angular指令中触发单击

问题描述 投票:0回答:1

我想知道如何在angularjs / 1.6.4指令中触发点击。

基本上这段代码部分工作:

app.directive('abc', function() {
  return {
    :
    :
    link: function(scope, element, attrs, controller) {
          :
       setTimeout(function(){element[0].click();}, 400);
          :
    }
  }
});

但问题是如果页面加载时间超过400毫秒。在这种情况下,它不起作用。因此,我更倾向于一种不依赖于指定超时时间的解决方案,而是一种无论页面加载多长时间都能工作的方法。

javascript angularjs triggers click directive
1个回答
0
投票

您应该在没有setTimeout参数的情况下使用milliseconds,因此它会在下一个tick中执行该函数。你可以阅读更多关于它here

app.directive('abc', function() {
  return {
    :
    :
    link: function(scope, element, attrs, controller) {
          :
       setTimeout(function(){element[0].click();});
          :
    }
  }
});
© www.soinside.com 2019 - 2024. All rights reserved.