AngularJS DOM 选择器 [英] AngularJS DOM selector
问题描述
我有一些自定义指令,它们使用 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 犯罪.
推荐答案
"jqLite"(定义在 angular.element page) 提供了children()
、parent()
、contents()
、等DOM遍历方法>find()
、next()
(但不是 previous()
).没有类似选择器的方法.
You might want to try JavaScript's querySelector.
这篇关于AngularJS DOM 选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!