通过范围 $id 获取 DOM 元素 [英] Get DOM element by scope $id

查看:37
本文介绍了通过范围 $id 获取 DOM 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我可以按元素获取范围:

I understand I can get the scope by element:

scope = angular.element($0).scope();
scope.$id; // "003"

如何获取反向:使用范围$id查找DOM元素,例如003?

How do I get reverse: Find the DOM element using the scope $id, such as 003?

我想这样做是为了调试目的.我的范围树显示了一些东西,我想确定它的来源.

I'd like to do this for debugging purposes. My scope tree shows something and I'd like to identify where it came from.

推荐答案

虽然不是很性感,但每个 dom 节点都有一个 ng-scope 类,所以你可以在技术上做这样的事情:

Although it's not very sexy each dom node gets a class ng-scope so you could tech do something like this maybe:

function getScope(id) {
var elem;
$('.ng-scope').each(function(){
    var s = angular.element(this).scope(),
        sid = s.$id;

    if(sid == id) {
        elem = this;
        return false; // stop looking at the rest
    }
});
return elem;
}

这篇关于通过范围 $id 获取 DOM 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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