简体   繁体   English

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

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

Hii i need to trigger a click in first elements #first > #second > #third > .element-for-trigger-click , see here 您好,我需要在第一个元素中触发点击#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>

Question : How can i get event from Left controller ,find .element-for-trigger-click and trigger a click on right controller with native js . 问题:我如何从Left控制器获取事件,找到.element-for-trigger-click并使用本机js触发对right控制器的单击。 在此处输入图片说明

Aro, 阿罗

so in following thread you can find information how to 'simulate' click event via native JS on given element (tough by id) 因此,在以下线程中,您可以找到有关如何通过给定元素上的本机JS“模拟”点击事件的信息(按ID划分)

How to simulate a click with JavaScript? 如何使用JavaScript模拟点击?

however I really would not recommend that kind of approach, especially if you use angularJS, as there are ways to do controller to controller communication with angular lib itself like: 但是我真的不推荐这种方法,特别是如果您使用angularJS,因为有一些方法可以与角度库本身进行控制器到控制器的通信,例如:

  • using of angular event bus ($emit, $broadcast and $on) 使用角度事件总线($ emit,$ broadcast和$ on)
  • using a services 使用服务
  • using pattern of state container like redux ( https://github.com/reactjs/redux ) 使用状态容器的模式,例如redux( https://github.com/reactjs/redux
  • and probably few more. 可能还有更多。

You can dispatch a click event with: 您可以通过以下方式调度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);

Btw, you wouldn't have to use .firstChild if you simply gave that element an ID like all your other elements. 顺便说一句,如果您像其他所有元素一样简单地给该元素一个ID,则不必使用.firstChild

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

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