我正在编写一个指令,允许单击外部元素来克隆其包含的元素之一的ui-sref,这样单击外部元素的行为与单击.cloned元素的行为相同
<div clone-click=".cloned">
...
<a class="cloned" ui-sref="root.a" ng-if="true">example</a>
<a class="cloned" ui-sref="root.b" ng-if="false">example</a>
...
<a ui-sref="root.c">elsewhere</a>
...
</div>我尝试了一个触发点击的属性指令
app.directive('cloneClick', function() {
return {
restrict: 'A',
scope: {
selector: '@cloneClick'
},
link: function(scope, element) {
element.click(function() {
element.find(scope.selector).not(':disabled').first().click();
})
}
};
})但这会导致无限循环或其他原因,并且不起作用。我怎么才能让它工作呢?或者有没有更好的方法来实现这一点?
发布于 2016-09-27 05:54:03
您没有考虑到事件冒泡。就像现在一样,子元素上的任何单击事件都已经冒泡到父元素,此时您将告诉它再次单击相同的元素……因此,如果您想要的目标被单击,则会出现无限循环
我的建议是防止事件在<a>上传播。
如果单击了<a>本身,则让浏览器处理重定向;如果单击了父对象的任何其他部分,则使用$location服务使用ui-sref生成的href值进行重定向。
类似于:
link: function(scope, element) {
var $link = element.find(scope.selector).not(':disabled').first();
// prevent bubbling on target link
$link.click(function(e) {
e.stopImmediatePropagation()
});
element.click(function(e) {
// make sure target link wasn't element clicked
if (e.target !== $link[0]) { // assumes no child tags in `<a>`
$location.url($link.attr('href'));
}
});
}您可能需要根据是否使用html5mode进行一些调整
编辑:在我写完这篇文章后,我想到你可以用点击<a>代替$location,因为事件传播(冒泡)仍然被阻止了
发布于 2016-09-28 07:58:08
<ANY clone-click=".is-clone-click:not(:disabled):not(.is-disabled)">
<a class="is-clone-click" ui-sref="root.example">example</a>
</ANY>I got it working like this。一些指针禁用的元素可以通过将它们的容器设置为e.target来点击,所以我在这些容器上添加了.is-no-clone-click来忽略它们。
app.directive('cloneClick', function() {
var angular = require('angular');
var ignore = '[href], [ui-sref], [ng-click], .is-no-clone-click, label, input, textarea, button, select, option, optgroup';
return {
restrict: 'A',
scope: {
selector: '@cloneClick'
},
link: function (scope, element) {
element.click(function(e) {
if (e.isTrigger) {
return;
}
var cloned = element.find(scope.selector).first();
var target = angular.element(e.target);
if (cloned.length && !cloned.is(target) && !target.is(ignore)) {
cloned.click();
}
});
}
};
});也可以通过鼠标悬停和类似CSS类添加光标
element.mouseover(function() {
element.toggleClass('is-pointer', !!element.has(scope.selector).length);
});但我最终没有使用这个指令,因为我能够创建一个CSS link masking solution来实际解决我想要做的事情。
https://stackoverflow.com/questions/39712445
复制相似问题