Angular中的.parents()? [英] .parents() in Angular?

查看:66
本文介绍了Angular中的.parents()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在Angular中模拟jQuery的 .parents()方法,而不必实际包含jQuery?

Is there a way I could emulate jQuery's .parents() method in Angular, without having to actually include jQuery?

最终目标是获得DOM元素的所有父母。

The final goal is to get all the parents of a DOM element.

编辑:我为什么需要这个?

Why I need this?

我正在创建一个指令(一个类似下拉菜单的小部件)。下拉菜单应该听取整个< body> 的点击次数并关闭自身(如果它已打开),如果在窗口小部件的区域外进行点击。

I'm creating a directive (a dropdown-like widget). The dropdown should listen the entire <body> for clicks and close itself (if it's open) if a click is made outside of the widget's area.

现在,我知道如何创建一个可以监听鼠标事件的简单指令,如下所示:

Now, I know how to create a simple directive that would listen for mouse events, like this one:

app.directive('mouseTrap', function() {
  return function(scope, elem) {
    elem.bind('click', function(event) {
      scope.$broadcast('click', { event: event } );
    });
  };
});

...我会像这样使用:< body mouse -trap ....>

...which I would then use like this: <body mouse-trap .... > and

$scope.$on('click', function(msg, obj) {
  console.log("click!");
});

这是我需要检查点击对象的任何父母是否是顶级的地方我的小部件的div,如果没有,关闭小部件。

That is where I need to check if any of the parents of the clicked object is the top-level div of my widget, and if not, close the widget.

推荐答案

编辑:虽然这个答案被接受了,这是不正确的。请参阅下面的评论和更好的答案。

Although this answer was accepted, it's not correct. See comment and better answers below.

看起来jQLite已经支持这个了,所以如果你正在使用angular,你应该能够做到这一点而不需要拉入JQuery本身。

It does look like jQLite supports this already so if you're using angular you should be able to do this without pulling in JQuery itself.

来自文档此处


支持的实例方法:(指向jQuery Docs的链接)当前实现了jQuery支持的操作的子集。所有这些都是使用jQuery的链式操作语法实现的。例如:

Supported instance methods: (links to jQuery Docs) A subset of the operations which jQuery supports are currently implemented. All of them are implemented using the chained operation syntax of jQuery. e.g:



$("div.foo").each(function() {
  $(this).removeClass("foo").addClass("bar");

});

$("div.bar, div.baz").css({ "border": "1px solid red"    }).children().addClass("child");



.parent([selector)]
.parents([selector)] 

这篇关于Angular中的.parents()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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