简体   繁体   English

如何克隆ui-sref

[英]How to clone ui-sref

I'm looking to write a directive that allows clicks on an outer element to clone the ui-sref of one of its contained elements such that clicking on the outer element behaves the same as clicking on the .cloned element 我正在寻找写一条指令,该指令允许单击外部元素以克隆其包含的元素之一的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>

I tried an attribute directive that triggers the click 我尝试了触发点击的属性指令

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();
            })
        }
    };
})

but this causes an infinite loop or something and doesn't work. 但这会导致无限循环或其他原因,并且不起作用。 How can I make it work? 我该如何运作? Or is there a better way to achieve this? 还是有更好的方法来实现这一目标?

You aren't taking into consideration event bubbling. 您没有考虑事件冒泡。 As it is now, any click event on the children will already bubble to the parent at which point you are telling it to click same element again ... thus infinite loop if the target you want is clicked 到目前为止,子级上的任何click事件都已经冒泡到父级,此时您要告诉它再次单击同一元素...从而无限循环(如果要单击的目标被单击)

My suggestion would be to prevent propagation of the event on the <a> . 我的建议是防止事件在<a>上传播。

If the <a> itself is clicked, let browser handle the redirect and if any other part of parent is clicked use $location service to redirect using the href value that ui-sref generates. 如果单击<a>本身,则让浏览器处理重定向,如果单击了父级的任何其他部分,请使用$location服务使用ui-sref生成的href值进行重定向。

Something like: 就像是:

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'));
    }
  });
}

You may need to adjust a bit depending on whether or not you are using html5mode 您可能需要根据是否使用html5mode进行一些调整

EDIT: it occurs to me after writing this that you may be able to trigger the click on the <a> instead of using $location since the event propagation (bubbling) is still prevented 编辑:我在写完这篇文章后想到,您可以触发<a>的点击而不是使用$location因为仍然可以防止事件传播(冒泡)

<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 . 我让它像这样工作 Some pointer disabled elements were able to be clicked thru making their container the e.target so I added .is-no-clone-click on those containers to ignore them. 通过将其容器设为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();
                }
            });
        }
    };
});

Cursor can also be added via mouseover and a CSS class for that like 光标也可以通过鼠标悬停和CSS类来添加

element.mouseover(function() {
    element.toggleClass('is-pointer', !!element.has(scope.selector).length);
});

But I didn't end up using this directive because I was able to create a CSS link masking solution to actually solve what I was trying to do instead. 但是我并没有最终使用此指令,因为我能够创建CSS链接屏蔽解决方案来实际解决我要尝试执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM