Angular:获取元素被编译时使用的作用域

Angular: Getting the scope that an element was compiled with

本文关键字:作用域 编译 获取 元素 Angular      更新时间:2023-09-26

假设我有一个directive,传入attribute为"contextMenuId "。"

在这个指令中,我现在执行:
contextMenu = $('[id*="' + iAttrs.contextMenuId+'"]');

还是……

contextMenu = angular.element(document.querySelector('[id*="' + iAttrs.contextMenuId+'"]'))

如果它们的作用不同,我希望有人能告诉我它们的区别,但这与我的答案是分开的。

现在,让我们看看我们的contextMenu:

<ul id="exampleContextMenu" context-menu-defs="contextMenuDefs" ></ul>

我想从我前面提到的指令访问数组contextMenuDefs,但这是来自我无法访问的完全不同的scope

我能以某种方式访问scope, contextMenu从我的指令内编译?谢谢!

可以使用

angular.element("<selector as jquery>")).scope();

在你的情况下:

var scopeOuther = angular.element('[id*="' + iAttrs.contextMenuId+'"]')).scope();