像在jQuery中一样如何在AgularJS中触发事件? [英] How to trigger an event in AgularJS as in jQuery?

查看:46
本文介绍了像在jQuery中一样如何在AgularJS中触发事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以仅使用AngularJS做类似 $(#container").trigger("click"); 的事情?如果无法进行触发,是否还有另一种方法可以管理此行为?

Is it possible to do something like $("#container").trigger("click"); using only AngularJS ? And if it's not possible to trigger, is there another way to manage this behavior ?

我有一个基本的示例,该示例在单击按钮时创建一个框.创建的框是可拖动的.现在,当前用户必须单击按钮(创建框),然后再次单击以拖动框.我希望用户只需单击一次即可:创建框,然后触发框的拖动事件,以便用户不必再次单击.

I have a basic example that creates a box on a button click. The created box is draggable. Now, currently the user has to click on the button (to create the box) and click again to drag the box. I would like the user to simply click once : to create the box and then trigger the drag event of the box so that the user doesn't have to click again.

var app = angular.module("app", []);

// This directive is attached to the button and makes it create a box every time the is a 'mousedown' event
app.directive("boxCreator", function($document, $compile) {
    return {
        restrict: 'A',
        link: function($scope, $element, $attrs) {
            $element.bind("mousedown", function($event) {
                var newNode = $compile('<div class="box" drag></div>')($scope);

                newNode.css({
                    top: $event.pageY - 25 + "px",
                    left: $event.pageX - 25 + "px"
                    });

                angular.element($document[0].body).append(newNode);
                // I'd like to do something like : '$(newNode).trigger("mousedown");'
            });
        }
    }
});

// Makes the binded element draggable
app.directive("drag", function($document) {
    return function(scope, element, attr) {
        var startX = 0, startY = 0, x = event.pageX - 25, y = event.pageY - 25;

        element.css({
         position: 'absolute',
         cursor: 'pointer'
        });

        element.on('mousedown', function(event) {
          event.preventDefault();
          startX = event.pageX - x;
          startY = event.pageY - y;
          $document.on('mousemove', mousemove);
          $document.on('mouseup', mouseup);
        });

        function mousemove(event) {
          y = event.pageY - startY;
          x = event.pageX - startX;
          element.css({
            top: y + 'px',
            left:  x + 'px'
          });
        }

        function mouseup() {
          $document.off('mousemove', mousemove);
          $document.off('mouseup', mouseup);
        }
      };
    });

JSFIDDLE

JSFIDDLE

推荐答案

您要查看的文档为 angular.element ,这是angular的JQLite实现.

The documentation you want to look at is angular.element, which is angular's JQLite implementation.

从中可以看到,有一个 .triggerHandler()方法.

You can see from this that there is a .triggerHandler() method.

所以像这样:

angular.element(newNode).triggerHandler('mousedown');

将触发处理程序.唯一的是,它不包含任何事件数据,因此您还必须删除对 event.pageY 的依赖.我通过修改以下两行来粗略地做到这一点:

Will trigger the handler. The only thing is, this doesn't include any of the event data, so you will also have to remove the dependency on event.pageY. I did this crudely by modifying these two lines:

startX = (event.pageX - x) || 0;
startY = (event.pageY - y) || 0;

您还可以研究传递的参数,但我根本无法使它正常工作.

You could also look into passing params, but I couldn't get that to work at all.

希望这可以帮助您入门.

Hopefully this gets you started.

> 更新的小提琴

这篇关于像在jQuery中一样如何在AgularJS中触发事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆