如何克隆ui sref
How to clone ui-sref
我想写一个指令,允许点击外部元素来克隆其包含的元素之一的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();
})
}
};
})
但这会导致一个无限循环或其他什么,并且不起作用。我怎样才能让它工作?或者有更好的方法来实现这一点吗?
您没有考虑事件冒泡。和现在一样,子元素上的任何单击事件都将冒泡到父元素上,此时您将告诉它再次单击同一元素。。。因此,如果你想要的目标被点击,就会出现无限循环
我的建议是防止事件在<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
,因为事件传播(冒泡(仍然被阻止
<ANY clone-click=".is-clone-click:not(:disabled):not(.is-disabled)">
<a class="is-clone-click" ui-sref="root.example">example</a>
</ANY>
我让它这样工作。一些禁用指针的元素可以通过将其容器设置为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链接屏蔽解决方案来实际解决我想要做的事情。
相关文章:
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- Ionic + Angular ui-sref not generating href
- 将URL参数传递给ui sref
- TR 元素上的 UI-sref 不会在新选项卡中打开
- AngularJS ui路由器:ui sref和指令
- 基于观察者布尔值执行 ui-sref
- 输入提交按钮上的 UI-sref
- 如何在 ui-sref 标记中放置重新加载选项
- 将复杂对象传递给 ui-sref 参数
- Angular ui-sref href resolve completion callback
- 将两个相同的参数发送到 ui-sref
- 动态创建的 ui-sref 链接不起作用
- ui sref opts={reload:true}不重新加载控制器
- 如何通过element.append使ui sref动态工作
- 一个ui sref链接工作,其余的不工作;t
- 路由:鼠标悬停在ui sref上时可见的参数值在单击时消失
- ui-sref="articles.view({articleId: article._id})"
- 如何在HTML中传递带有javascript数据的URL,而不是ui sref
- 如何有条件地执行“ui-sref”
- Angular UI Router——当使用UI -sref导航到动态状态时,会得到双斜杠