![](/img/trans.png)
[英]Is there an angular directive that can restrict scope for an element in the DOM
[英]Limit scope of angular element to directive
我有一個指令,在這個指令中,我想操作滾動上的某些元素。 目前,我正在使用:
angular.element( document.getElementsByClassName('splash-content') );
很好,盡管看起來效率不高。
我真正想做的是將選擇器限制為調用該指令的元素的作用域(子元素)。 在jQuery中,我將使用element.find('.splash-content')
或element.children('.splash-content')
但在angularjs中,您似乎無法將選擇器添加到這兩種方法中。
那么,如何最好(最有效)地完成“成角度的”方式呢?
謝謝!
我創建了一個簡單的小提琴,展示了如何在不包括jQuery怪獸的情況下進行此類操作: http : //jsfiddle.net/ADukg/4769/
例如,將其放在指令中:
var childFind = function(children, className, results) {
for(var i=0; i<children.length; i++){
var child = angular.element(children[i]);
if(child.hasClass(className)){
results.push(child);
}
childFind(child.children(), className, results);
}
}
childFind(children, 'one', results);
顯然,這種事情遠不及jQuery提供的方法那么廣泛或完整,但是,如果您只需要一些小的一次性功能,我會選擇在各處添加一個簡單的輔助函數,而無需jQuery。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.