angular.js从$ evalAsync里面指令获取元素 [英] angular.js Getting the element from inside $evalAsync in directive
问题描述
我发现我使用的范围。$ evalAsync指令内不少。主要做了所需要的所有模板DOM的东西/ jQuery插件{{瓦尔}}编译。
I'm finding that I'm using scope.$evalAsync inside a directive quite a lot. Mainly to do DOM stuff/jquery plugins that need all the template {{vars}} compiled.
我可以在对象范围从$ evalAsync元素内,但没有得到。在最新的问题的情况下,我操纵在获取与呈现ngRepeat的元素。我目前基于范围的对象组成例如一个jQuery选择获得的元素
I can get at the scope object from inside $evalAsync but not the element. In latest case in question, I'm manipulating an element that gets rendered with an ngRepeat. I'm currently getting the element by composing a jquery selector based on the scope object e.g.
scope.$evalAsync(function (scope) {
$("#item-" + scope.id).runJQplugin();
})
虽然这个作品,对我来说这将是更直观,能够做到这一点。
Although this works, to me it would be more intuitive to be able to do this
scope.$evalAsync(function (scope,element) {
element.runJQplugin();
})
我是不是接近这一权利或具有我误解与指令的东西根本?
Am I approaching this right or have I misunderstood something fundamental with directives?
推荐答案
您始终能够访问从链接的元素和指令通过的闭合的范围控制器。因此,在链接功能:
You always have access to the element from the link and the controller of a directive through the closure scope. So in link function:
link: function(scope, elem, attrs) {
...
scope.$evalAsync(function(scope) {
elem.runJQplugin();
});
...
},
控制器(你需要指定特殊 $元素
依赖):
controller: ["$scope", "$element", function($scope, $element) {
...
scope.$evalAsync(function(scope) {
$element.runJQplugin();
});
...
}],
这篇关于angular.js从$ evalAsync里面指令获取元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!