[英]How to select an element with a specific id from a directive's linking function's 'element' parameter, using jQuery rather than angular's jqLite?
这是指令的模板:
(如果我理解正确的话,这就是链接功能的'element'参数所提供的,不是吗?)
template: '<div class="panel">' +
'<div id="lh" title=""></div>' +
'<div id="rh" title=""></div>' +
'</div>',
我已经读到,当在Angular之前加载jQuery时,jQuery优先于jqLight。 牢记这一点,这是我尝试从链接功能中选择标有“ lr” ID的div并设置其“ title”属性的方法:
link: function(scope, element, attrs) {
element.$('#link').attr("title", attrs.title);
}
这不起作用,就好像没有jQuery一样。
在利用jQuery的同时,如何以“角度”方式做到这一点?
感谢您的时间。
杰瑞德
在jQuery中,您可以选择从开始查找的地方设置根。
$(selector[, root])
在你的情况下
$("#lr", element)
同样值得注意的是,在任何给定时间,页面上应该只存在一个具有特定ID的元素,因此$("#lr")
也应该起作用。
基本上,您需要获取模板的HTML,然后使用jqLite(或jQuery)对象对其进行转换。
link: function (scope, element, attrs) {
var html = element[0].innerHTML;
var e = angular.element(html).find("#lr").attr('title', 'ABC');
element.replaceWith(e);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.