【问题标题】:How to clone ui-sref如何克隆 ui-sref
【发布时间】:2016-09-28 07:58:08
【问题描述】:

我希望编写一个指令,允许单击外部元素以克隆其包含元素之一的 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();
            })
        }
    };
})

但这会导致无限循环或其他东西并且不起作用。我怎样才能让它工作?还是有更好的方法来实现这一点?

【问题讨论】:

    标签: javascript angularjs angularjs-directive click ui-sref


    【解决方案1】:

    您没有考虑事件冒泡。就像现在一样,子级上的任何点击事件都会冒泡到父级,此时你告诉它再次点击相同的元素......因此如果你想要的目标被点击则无限循环

    我的建议是防止事件在 &lt;a&gt; 上传播。

    如果&lt;a&gt; 本身被点击,则让浏览器处理重定向,如果点击父级的任何其他部分,则使用$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 进行一些调整

    编辑:在写完这篇文章后,我突然想到你可以触发点击&lt;a&gt; 而不是使用$location,因为事件传播(冒泡)仍然被阻止

    【讨论】:

    【解决方案2】:
    <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 来实际解决我想要做的事情。

    【讨论】:

      猜你喜欢
      • 2018-07-25
      • 1970-01-01
      • 2015-10-30
      • 2010-10-17
      • 2019-03-22
      • 2011-01-28
      • 2012-02-06
      相关资源
      最近更新 更多