繁体   English   中英

在控制器中的ng-repeat中触发对第一个元素的第一个元素的单击

[英]Trigger a click to first element of first element in ng-repeat in controller

您好,我需要在第一个元素中触发点击#first> #second> #third> .element-for-trigger-click,请参见此处

<div id="first" ng-repeat="competition in prematchGameViewData track by competition.id">
    <div id="header">...</div>
    <div id="details">...</div>


    <div id="second" ng-repeat="(groupDate, groupGames) in competition.gamesGroupedByDate">     
        <div id="third" ng-repeat="prematchGame in groupGames track by prematchGame.id">
            <div class="element-for-trigger-click"></div>
            ...
        </div>
    </div>
</div>

问题:我如何从Left控制器获取事件,找到.element-for-trigger-click并使用本机js触发对right控制器的单击。 在此处输入图片说明

阿罗

因此,在以下线程中,您可以找到有关如何通过给定元素上的本机JS“模拟”点击事件的信息(按ID划分)

如何使用JavaScript模拟点击?

但是我真的不推荐这种方法,特别是如果您使用angularJS,因为有一些方法可以与角度库本身进行控制器到控制器的通信,例如:

  • 使用角度事件总线($ emit,$ broadcast和$ on)
  • 使用服务
  • 使用状态容器的模式,例如redux( https://github.com/reactjs/redux
  • 可能还有更多。

您可以通过以下方式调度click事件:

var evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 1, 0, 0, 0, 0,
    false, false, false, false, 0, null);

var target = document.getElementById('third').firstChild;
target.dispatchEvent(evt);

顺便说一句,如果您像其他所有元素一样简单地给该元素一个ID,则不必使用.firstChild

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM