简体   繁体   English

Mootools .addEvent无法进入ng-repeat内部?

[英]Mootools .addEvent can't get inside ng-repeat?

So I'm trying to use mootools to create a drag/drop cart as seen here: demo . 因此,我尝试使用mootools创建拖放车,如此处所示: demo I can get there code working just fine, but when I try to edit their html to look like the html beloe, it stops working. 我可以到那里使代码正常工作,但是当我尝试编辑其html以使其看起来像html beloe时,它将停止工作。 The css still applies as one would expect, and the div outside of the ng-repeat works fine. css仍然可以像人们期望的那样适用,并且ng-repeat之外的div工作正常。 However, I'm pretty sure that the .addEvent in the mootools javascript can't 'find' the .item within the ng-repeat, so I am unable to grab any of the .items in ng-repeat. 但是,我非常确定mootools javascript中的.addEvent无法在ng-repeat中“查找” .item,因此我无法在ng-repeat中获取任何.items。

Any ideas? 有任何想法吗?

UPDATE: OK, it appears that the problem involves the domready event in the javascript. 更新:好的,看来问题出在javascript中的domready事件。 The angularjs hasn't loaded when that fires. 触发时未加载angularjs。 I tried the load event, but this also fires before angularjs loads. 我尝试了load事件,但是在angularjs加载之前也会触发。 Any other ideas for events? 关于活动还有其他想法吗? I'm using mousedown right now, but this means you have to click on one of the images before you can start dragging them, which is not optimal. 我现在正在使用mousedown,但这意味着您必须先单击其中一张图像,然后才能开始拖动它们,这不是最佳选择。 In addition, the mousedown event seems to cause strange duplicates to appear in the cart sometimes, so I'll need to find a better method. 此外,mousedown事件似乎有时会导致奇怪的重复出现在购物车中,因此我需要找到一种更好的方法。

<div id="items">
    <div class="item">
        <p>test</p>
    </div>
  <div ng-repeat="post in posts">

    <div class="item">
        <img class="picImage" src="/static/imgs/{{post.picture}}" alt="Smiley face" height="128" width="128">
        <p>Shirt 1</p>
    </div>
  </div>
</div>

depends on how you attach the draggables. 取决于您如何连接可拖动对象。 thing is, before ng-repeat renders through the $scope.$apply etc lifecycle for this part of the app, these elements won't be there. 问题是,在此部分的ng-repeat通过$scope.$apply等生命周期呈现之前,这些元素将不存在。

// this code is wrong. 
$$('.item').addEvent('mousedown', function(event){
    event.stop();
... 
});

additionally, if the collection changes, mootools won't know to add the new click handlers. 另外,如果集合发生变化,mootools将不会添加新的点击处理程序。

you don't want to be waiting for content change by ng and re-applying mootools events, not a good idea. 您不想等待ng更改内容并重新应用mootools事件,这不是一个好主意。

you can either use: https://docs.angularjs.org/api/ng/directive/ngMousedown to bind the events to the collection elements, add a $scope.onMouseDown and move the logic there, letting angular deal with it internally: 您可以使用: https : $scope.onMouseDown将事件绑定到集合元素,添加$scope.onMouseDown并在其中移动逻辑,以便在内部进行角度处理:

<div ng-repeat="post in posts">
    <div class="item" ng-mousedown="onMouseDown($event, post, $index)">
        <img class="picImage" src="/static/imgs/{{post.picture}}" alt="Smiley face" height="128" width="128">
        <p>Shirt 1</p>
    </div>
</div>

with something like 用类似的东西

$scope.onMouseDown = function($event, post, $index){
   // clone and do stuff


   // if you want to disable stuff, modify `post` etc, potentially can do $scope.$digest / $apply ...
};

... or you can use event delegation and bind like so: ...或者您可以使用事件委托并像这样进行绑定:

$('items').addEvent('mousedown:relay(.items)', function(event){ 

    // ... do normal logic but no access to ng model here. can get dirty
});

event delegation (as above via relay) will work fine, as long as angular does not decide to re-render div#items due to whatever renders that part of your app. 事件委托(如上所述,通过中继)将正常工作,只要angular决定由于任何呈现应用程序的部分而决定不重新呈现div#items if it does, it will destroy the saved events. 如果这样做,它将破坏保存的事件。 it also means you need to be able to see this element first to bind to it via mootools. 这也意味着您需要能够首先看到该元素,才能通过mootools绑定到该元素。

You may want to bind the event to your ng-app div to be safe and delegate to this one: 为了安全起见,您可能需要将该事件绑定到ng-app div并委托给该事件:

document.getElement('[ng-app]').addEvent('mousedown:relay(#items div.items)', fn);

Have you tried hooking on to the $viewContentLoaded event? 您是否尝试过挂接到$ viewContentLoaded事件?

They talk about it here: AngularJs event to call after content is loaded 他们在这里谈论它: 加载内容后调用AngularJs事件

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

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