我在使用angularjs指令查找带有注入(inject)的angular元素的子DOM元素时遇到麻烦。
例如,我有一个这样的指令:
myApp.directive('test', function () {
return {
restrict: "A",
link: function (scope, elm, attr) {
var look = elm.find('#findme');
elm.addClass("addedClass");
console.log(look);
}
};
});
和HTML,例如:
<div ng-app="myApp">
<div test>TEST Div
<div id="findme"></div>
</div>
</div>
我可以通过添加一个类来访问该元素。
但是,尝试访问子元素会在var外观中生成一个空数组。
JSFiddle demo is here。
为什么如此琐碎的事情无法正常工作?
最佳答案
从docs on angular.element
:
因此,如果您不将jQuery与Angular结合使用,而是依靠其jqlite实现,则不能执行elm.find('#someid')
。
您确实可以访问children()
,contents()
和data()
实现,因此通常可以找到解决方法。
关于javascript - jQuery find()方法在AngularJS指令中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14865965/