我正在使用以下代码成功编译指令:
var element, $injector, $compile, link, scope;
element = angular.element(document.getElementById(#whatever));
$injector = element.injector();
$compile = $injector.get('$compile');
link = $compile(angular.element('<my-directive></my-directive>'));
scope = element.scope();
scope.foo = 'foo'; // I want to place this on the isolate scope!
element.append(link(scope));
该指令具有隔离范围。上面的 scope
变量为我提供了父元素的范围。如何获取指令的隔离范围?
最佳答案
Angular element
对象具有一些预定义的函数,例如 isolatedScope
。挑战在于在正确的元素上调用它。像这样的东西有效:
angular.element(link(scope)).isolateScope()
在这里,您可以获取该指令的已编译+链接的 html 片段,然后调用 isolatedScope()
函数来获取隔离范围。
关于javascript - 访问已编译指令的隔离范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31721285/