0

I wonder how I can trigger a click from within an angularjs/1.6.4 directive.

Basically this code works partially:

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

However the problem is if the page takes longer than 400ms to load. In this case it does not work. Therefore I would prefer a solution that didn't depend on a specified timeout period but a method that worked no matter how long the page took to load.

Zobia Kanwal
  • 4,085
  • 4
  • 15
  • 38
Hurve
  • 173
  • 1
  • 2
  • 12

1 Answers1

0

You should use setTimeout without a milliseconds parameter, so it execute the function in the next tick. You can read more about it here.

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