AngularJS DOM 选择器 [英] AngularJS DOM selector

查看:31
本文介绍了AngularJS DOM 选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些自定义指令,它们使用 jQuery 实现动画效果(angular 的内置 ngShow/ngHide 等功能强大,但并不漂亮).我想我记得在文档某处读过 angular 有它自己的 DOM 选择器(类似于 angular.export()angular.select()),我应该使用它$(SELECTOR);但是我现在找不到了.

I've got a few custom directives that use jQuery for animation effects (angular's built-in ngShow/ngHide and the like are functional, but not pretty). I think I remember reading in the documentation somewhere that angular has its own DOM selector (something like angular.export() or angular.select()) that I should use instead of $(SELECTOR); however I can't find it now.

我正在做这样的事情:

//view
<div scroll-to="element"> //`element` is set via ng-click
  …
</div>

//directive
link: function(scope, elm, attrs)
{

  scope.$watch(attrs.scrollTo, function scrollToAction(newValue,oldValue)
  {
    if ( newValue !== oldValue )
    {
      elm.animate({
        scrollTop:
          $('#'+newValue).offset().top //replace jquery selector with angular's
          - elm.offset().top
          + elm.scrollTop()
      });
    }
  });

}

//指示链接:功能(范围,榆树,属性){scope.$watch(attrs.scrollTo, 函数 scrollToAction(newValue,oldValue){if ( newValue !== oldValue ){榆树动画({滚动顶部:$('#'+newValue).offset().top//用angular的替换jquery选择器- elm.offset().top+ elm.scrollTop()});}});}

I'm not really manipulating $('#'+newValue), just retrieving info about it, so I don't think I'm committing a crime against Angular.

我并不是真的在操纵 $('#'+newValue),只是检索有关它的信息,所以我不认为我在对 Angular 犯罪.

解决方案

推荐答案

"jqLit​​e"(定义在 angular.element page) 提供了children()parent()contents()等DOM遍历方法>find()next()(但不是 previous()).没有类似选择器的方法.

You might want to try JavaScript's querySelector.

这篇关于AngularJS DOM 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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