如何在href设置后模拟angularjs中的锚点击?

时间:2016-03-23 18:54:15

标签: javascript angularjs click

我正在尝试导出到excel文件,除了最简单的部分外,我已经完成了所有工作;在设置文件的href之后模拟锚点击。

我正在设置这样的属性:

// This is inside a directive link function
scope.exportXLS() {

  var html = 'A long piece of html string generated from data';

  $('#export-xls-' + scope.graphId).attr('href', URL.createObjectURL(new Blob([html])));
}

导致:

<li ng-click="exportXLS()">
  <a id="export-xls-0" class="eo-xls" download="exported-graph.xls" type="text/csv" href="blob:http%3A//localhost%3A3000/20b7d696-b1c2-41b3-a570-f7389ff3d2fb">Export to XLS</a>
</li>

然后我等待$digest周期在Angular $timeout的帮助下完成运行,然后再调用click()以及使用event.stopPropagation()来防止锚定点击触发exportXLS()导致无限混乱:

$timeout(function() {
  $('#export-xls-' + scope.graphId).click(function(event) {
    event.stopPropagation(); 
  });
});

但是没有发生.. href正在设置但是它没有下载文件,因为点击似乎不起作用。我知道click()不应该与锚点一起使用但是我看到很多答案说你可以使用它并且它工作得非常好等等但不是在这种情况下。

值得注意的是,我使用的是chrome而不是firefox,我听说它还有模拟点击的问题。

如何在设置href属性后模拟点击?

0 个答案:

没有答案
相关问题