jQuery find() 方法在 AngularJS 指令中不起作用 [英] jQuery find() method not working in AngularJS directive
问题描述
我在使用 angularjs 指令查找带有注入的 angular 元素的子 DOM 元素时遇到问题.
例如我有一个这样的指令:
myApp.directive('test', function () {返回 {限制:A",链接:函数(范围,榆树,属性){var look = elm.find('#findme');elm.addClass("addClass");控制台日志(看);}};});
和 HTML,例如:
<div test>TEST Div<div id="findme"></div>
我可以访问通过向其添加类来提供的元素.但是,尝试访问子元素会在 var 外观中产生一个空数组.
为什么这么琐碎的事情不能正常工作?
来自 angular.element<上的文档/code>
:
find()
- 仅限于按标签名称查找
因此,如果您不将 jQuery 与 Angular 一起使用,而是依赖其 jqlite 实现,则无法执行 elm.find('#someid')
.
您确实可以访问 children()
、contents()
和 data()
实现,因此您通常可以找到一种方法围绕它.
I am having trouble with angularjs directives finding child DOM elements with the injected angular element.
For example I have a directive like so:
myApp.directive('test', function () {
return {
restrict: "A",
link: function (scope, elm, attr) {
var look = elm.find('#findme');
elm.addClass("addedClass");
console.log(look);
}
};
});
and HTML such as :
<div ng-app="myApp">
<div test>TEST Div
<div id="findme"></div>
</div>
</div>
I have access to the element which is proffed by adding a class to it. However attempting to access a child element produces an empty array in the var look.
Why is something so trivial not working properly?
From the docs on angular.element
:
find()
- Limited to lookups by tag name
So if you're not using jQuery with Angular, but relying upon its jqlite implementation, you can't do elm.find('#someid')
.
You do have access to children()
, contents()
, and data()
implementations, so you can usually find a way around it.
这篇关于jQuery find() 方法在 AngularJS 指令中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!