简体   繁体   English

请参阅指令angularjs中的HTML元素

[英]Refer to HTML element from directive angularjs

I'm doing some DOM manipulation by adding function on click event inside my directive. 我通过在指令中的click事件上添加函数来进行一些DOM操作。

Basically it's animation of moving 4 little boxes to right and showing 4 "new" boxes sliding from left side. 基本上,它是向右移动4个小盒子并显示从左侧滑动4个“新”盒子的动画。 For real that's just 20 boxes in line with hidden overflow that you can see only 4 at time and on click it's just adding margin-left: -270px to pretend horizontal scrolling. 实际上,只有20个框与隐藏的溢出线margin-left: -270px ,您一次只能看到4个框,而单击时只是margin-left: -270px添加margin-left: -270px来假装水平滚动。

Currently I'm "catching" this 20 elements with 目前,我正在“捕捉”这20个元素

document.querySelectorAll(".last-added-element").forEach(function(elem) {

and this solution is working fine untill I use my directive twice at same page (same view). 直到我在同一页面(相同视图)两次使用指令后,此解决方案才能正常工作。 It's then scrolling boxes in both direcrives when I execute function. 然后,当我执行功能时,将滚动两个目录中的框。

Can I somehow scope above line ( querySelectorAll method) to get NodeList of results but only from its template no entire DOM? 我可以以某种方式在行上方( querySelectorAll方法)进行作用域以获取结果的NodeList,但仅从其模板获取整个DOM吗?

link in directive exposes element the directive is called on so query from that element 指令中的link公开了调用该指令的元素,因此从该元素进行查询

link:function(scope, element, attrs){
   // element is a jQlite object
   element[0].querySelector('.someClass').doSomething 
}

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

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