首页 > 解决方案 > 弹出窗口链接在窗口以及浏览器选项卡中启动

问题描述

我正在开发一个使用 Laravel 和 AngularJS 构建的网站。我想在弹出窗口中打开某个链接。javascript代码是

function popitup(url) {
    newwindow=window.open(url,'test','height=400,width=550');
    if (window.focus) {newwindow.focus()}
    return false;
    }

链接是

<a ui-sref="test({id:test.id})" class="btn btn-primary fright" onclick="return popitup(this.href)" oncontextmenu="return false;">Resume</a>

当我单击按钮时,弹出窗口工作正常,但链接也会在我单击它的选项卡中打开,但我不希望它打开。有没有办法做到这一点?

标签: javascriptangularjsangular-ui-routerpopupwindowui-sref

解决方案


我猜ui-sref还会绑定一个点击事件,并且该事件将在您之前触发。

您可以跳过 ui-sref 并将 url 放在数据属性中或 onclick 属性中。您可以改为使用 $state.href() 获取 url。那么问题可能会消失。

编辑

您可以将其绑定到范围函数并一起跳过 onclick。像这样的东西:

在控制器中(还要确保首先在控制器中包含 $state):

$scope.popitup = function(stateName, options) {
 var url = $state.href(stateName, options);
 newwindow=window.open(url,'test','height=400,width=550');
 if (window.focus) {newwindow.focus()}
}

在 HTML 中,您可以使用状态名称及其参数调用 popuitup 函数:

<a ng-click="popitup('test', {id:test.id})" 
   class="btn btn-primary fright" oncontextmenu="return false;">Resume</a>

另请参阅state.href的文档。


推荐阅读