Angularjs angular.js元素没有方法querySelector

Angularjs angular.js元素没有方法querySelector,angularjs,jquery-selectors,Angularjs,Jquery Selectors,我试图从angular中获取一个子元素作为指令中的对象,所以 link: function(scope,elem,attrs){ var resizeBar = elem.querySelector('.resize-bar'); 但这是一个很好的结果 <div class="resize-bar">Move</div> 移动 这不是我需要的,我需要调整大小栏的html对象 有人知道一个很好的方法吗 elem的控制台输出为 : div.favor-layout

我试图从angular中获取一个子元素作为指令中的对象,所以

link: function(scope,elem,attrs){ var resizeBar = elem.querySelector('.resize-bar'); 但这是一个很好的结果

<div class="resize-bar">Move</div>
移动
这不是我需要的,我需要调整大小栏的html对象

有人知道一个很好的方法吗

elem的控制台输出为

: div.favor-layout.favor-layout-sideways accessKey: "" align: "" attributes: NamedNodeMap baseURI: "file:///C:/Users/pete/projects/favor/favor-layout/index.html" childElementCount: 1 childNodes: NodeList[2] children: HTMLCollection[1] classList: DOMTokenList className: "favor-layout favor-layout-sideways" clientHeight: 200 clientLeft: 0 clientTop: 0 clientWidth: 913 contentEditable: "inherit" dataset: DOMStringMap dir: "" draggable: false firstChild: div.favor-layout-container firstElementChild: div.favor-layout-container hidden: false id: "" innerHTML: "↵ ↵ This gets wrapped.↵ ↵ move↵↵" innerText: "This gets wrapped.↵move" isContentEditable: false lang: "" lastChild: text lastElementChild: div.favor-layout-container localName: "div" namespaceURI: "http://www.w3.org/1999/xhtml" nextElementSibling: null nextSibling: text nodeName: "DIV" nodeType: 1 nodeValue: null offsetHeight: 200 offsetLeft: 8 offsetParent: body.ng-scope offsetTop: 8 offsetWidth: 913 onabort: null onbeforecopy: null onbeforecut: null onbeforepaste: null onblur: null oncancel: null oncanplay: null oncanplaythrough: null onchange: null onclick: null onclose: null oncontextmenu: null oncopy: null oncuechange: null oncut: null ondblclick: null ondrag: null ondragend: null ondragenter: null ondragleave: null ondragover: null ondragstart: null ondrop: null ondurationchange: null onemptied: null onended: null onerror: null onfocus: null oninput: null oninvalid: null onkeydown: null onkeypress: null onkeyup: null onload: null onloadeddata: null onloadedmetadata: null onloadstart: null onmousedown: null onmouseenter: null onmouseleave: null onmousemove: null onmouseout: null onmouseover: null onmouseup: null onmousewheel: null onpaste: null onpause: null onplay: null onplaying: null onprogress: null onratechange: null onreset: null onscroll: null onsearch: null onseeked: null onseeking: null onselect: null onselectstart: null onshow: null onstalled: null onsubmit: null onsuspend: null ontimeupdate: null onvolumechange: null onwaiting: null onwebkitfullscreenchange: null onwebkitfullscreenerror: null onwheel: null outerHTML: "↵ ↵ This gets wrapped.↵ ↵ move↵↵" outerText: "This gets wrapped.↵move" ownerDocument: document parentElement: body.ng-scope parentNode: body.ng-scope prefix: null previousElementSibling: null previousSibling: text scrollHeight: 200 scrollLeft: 0 scrollTop: 0 scrollWidth: 913 spellcheck: true style: CSSStyleDeclaration tabIndex: -1 tagName: "DIV" textContent: "↵ ↵ This gets wrapped.↵ ↵ move↵↵" title: "" translate: true webkitPseudo: "" webkitShadowRoot: null webkitdropzone: "" __proto__: HTMLDivElement context: div.favor-layout.favor-layout-sideways length: 1 __proto__: Object[0] :div.favor-layout.favor-layout-sideways accessKey:“ 对齐:“” 属性:NamedNodeMap baseURI:“file:///C:/Users/pete/projects/favor/favor-layout/index.html“ childElementCount:1 子节点:节点列表[2] 子项:HTMLCollection[1] classList:DOMTokenList 类名:“偏好布局偏好侧向布局” 客户人数:200 clientLeft:0 客户端:0 客户宽度:913 contentEditable:“继承” 数据集:DOMStringMap 署长:“” 可拖动:错误 第一个子项:div.favor-layout-container firstElementChild:div.favor-layout-container 隐藏:假 id:“ innerHTML:“↵ ↵ 这个包好了。↵ ↵ 移动↵↵" innerText:“这将被包装。↵移动“ isContentEditable:false 朗:“ 最后一个孩子:文本 lastElementChild:div.favor-layout-container localName:“div” 名称空间URI:“http://www.w3.org/1999/xhtml" nextElementSibling:空 下一步:文本 nodeName:“DIV” 节点类型:1 nodeValue:null 越洋:200 偏移量:8 offsetParent:body.ng-scope 抵销:8 网外宽度:913 onabort:null onbeforecopy:空 onbeforecut:null onbeforeplaste:null onblur:null oncancel:null oncanplay:null oncanplaythrough:null onchange:null onclick:null onclose:null oncontextmenu:空 oncopy:null oncuechange:null oncut:null ondblclick:null ondrag:null ondragend:null ondragenter:null ondragleave:null ondragover:null ondragstart:null ondrop:null ondurationchange:null OneEmptied:null onended:null onerror:null onfocus:null oninput:null oninvalid:null onkeydown:null onkeypress:null onkeyup:null onload:null onloadeddata:空 onloadedmetadata:空 onloadstart:null onmousedown:null onMouseCenter:null onmouseleave:null onmousemove:null onmouseout:null onmouseover:null onmouseup:null onmouseheel:null onpaste:null onpause:null onplay:null 正在播放:空 onprogress:null onratechange:null onreset:null onscroll:null onsearch:null onseek:null 搜索:空 onselect:null onselectstart:null onshow:null 已安装:空 onsubmit:null onsuspend:null ontimeupdate:null onvolumechange:null onwaiting:null onwebkitfullscreenchange:null onwebkitfullscreenerror:空 onwheel:null 外部TML:“↵ ↵ 这个包好了。↵ ↵ 移动↵↵" outerText:“这个包起来了。↵移动“ 所有者文档:文档 parentElement:body.ng-scope 父节点:body.ng-scope 前缀:空 previousElementSibling:null 前兄弟:文本 卷轴高度:200 左:0 滚动顶部:0 滚动宽度:913 拼写检查:对 样式:CSSStyleDeclaration tabIndex:-1 标记名:“DIV” 文本内容:“↵ ↵ 这个包好了。↵ ↵ 移动↵↵" 标题:“ 翻译:是的 webkitPseudo:“ webkitShadowRoot:null webkitdropzone:“ __原型:HTMLDivElement 上下文:div.follow-layout.follow-layout-sideways 长度:1 __原型:对象[0]
.querySelector()
文档
的一种方法。但是,既然
元素
已经启动,您可以执行以下操作:

var currentElemClass = "." + elem.className.replace(/\s/g, ".") + " ";
var resizeBar = document.querySelector(currentElemClass + ".resize-bar");
这样做的目的是获取当前元素的类名,然后将其添加到
document.querySelector
的开头,以便获得准确的查询

例如,将其应用于
div.favor-layout.favor layout sideways
currentElemClass
变为
.favor layout.favor layout sideways
,因此最终您会得到:

document.querySelector(".favor-layout.favor-layout-sideways .resize-bar");

这是一个有效的查询。

试试这个,返回值是element,不是angular element

var resizeBar = elem[0].querySelector('.resize-bar');

如果要查找指令模板中驻留的元素,可以在
链接中使用该元素:

var divElement = angular.element(element[0].querySelector('#find-me'));
它将查找
#find me
id。但它只在指令的模板中查找。因此,它不会在指令之外返回任何具有相同id的元素,如
文档。querySelector(“#find me”)

因此,如果在指令中使用它,它将如下所示:

angular.module('app').directive('myDirective', function(){
    return{
        link: function(scope, element, attributes){
            var divElement = angular.element(element[0].querySelector('#find-me'));
        },
        template: function(element, attributes){
            return '<div id="find-me">' +
                        'Look at my directive' +
                    '</div>';
        }
    }
});
angular.module('app').directive('myDirective',function(){
返回{
链接:功能(范围、元素、属性){
var divElement=angular.element(元素[0]。查询选择器(“#找到我”);
},
模板:函数(元素、属性){
返回“”+
“看我的指令”+
'';
}
}
});

谢谢,我正在尝试在没有jQuery的情况下执行此操作,但可能会放弃。您的解决方案的问题是,当我将resizeBar放入控制台时,它会给我html字符串,而不是对象。您可以粘贴
console.log(elem)的响应吗
在您的指令中?添加了元素的console.log这是一个好主意knrz,我会仔细研究一下,但我遇到的问题是,我将有许多div具有相同的类组,这将使它们全部可用。我会考虑添加一个id,但我可能没有与每个div相关的唯一数据来创建id。querySelector是一种元素方法,因此它不仅适用于文档,还可以编写一个示例,我支持这个答案。
angular.module('app').directive('myDirective', function(){
    return{
        link: function(scope, element, attributes){
            var divElement = angular.element(element[0].querySelector('#find-me'));
        },
        template: function(element, attributes){
            return '<div id="find-me">' +
                        'Look at my directive' +
                    '</div>';
        }
    }
});