按住Ctrl键单击新标签页中的打开页面

Tar*_*nfx 8 angularjs angularjs-ng-click

基本上我希望我的应用程序在用户按住Ctrl(在Linux窗口上),Cmd(在OSX上)时在新选项卡/页面中打开链接.什么是路要走.

注意:我无法摆脱ng-click.应用程序在范围内使用它进行各种方法调用.

小智 8

试试这个:

在HTML中,

<div ng-click="gotoPageOrFunc($event)"></div>
Run Code Online (Sandbox Code Playgroud)

在JS中,

$scope.gotoPageOrFunc = function(event){
    if (event.ctrlKey==1){
        // Use windows.location or your link.
    }else{
        // Your actual functionalities.
    }
}
Run Code Online (Sandbox Code Playgroud)


Mir*_*age 0

使用指令。

angular.module('myapp').directive('newtab', function() {
    return {
        restrict: 'AC',
        link: function($scope, $elem) {
            var target = $elem.attr('target');

            $elem.on('click', function(e) {
                target = $elem.attr('target');
                $elem.attr('target', '_blank');
            });

            $elem.on('blur', function() {
                $elem.attr('target', target);
            });
        }
    };
});
Run Code Online (Sandbox Code Playgroud)

// 更新

用法

<a href="/my/page?foo=bar" class="newtab">xoxo<a/>
or
<a href="/my/page?foo=bar" newtab>xoxo</a>
or
<a href="/my/page?foo=bar" data-newtab>xoxo</a>
Run Code Online (Sandbox Code Playgroud)

//更新2

<a href="/my/page?foo=bar" 
   data-ng-click="myControllerMethod()" class="newtab">xoxo<a/>
or
<a href="/my/page?foo=bar" 
   data-ng-click="myControllerMethod()" newtab>xoxo</a>
or
<a href="/my/page?foo=bar" 
   data-ng-click="myControllerMethod()" data-newtab>xoxo</a>
Run Code Online (Sandbox Code Playgroud)