jQuery find() 方法在 AngularJS 指令中不起作用 [英] jQuery find() method not working in AngularJS directive

查看:25
本文介绍了jQuery find() 方法在 AngularJS 指令中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 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 外观中产生一个空数组.

JSFiddle 演示在这里.

为什么这么琐碎的事情不能正常工作?

解决方案

来自 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.

JSFiddle demo is here.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆