Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript AngularJS-通过控制器有条件路由时在新选项卡中打开链接_Javascript_Angularjs_Tabs_Angularjs Ng Click - Fatal编程技术网

Javascript AngularJS-通过控制器有条件路由时在新选项卡中打开链接

Javascript AngularJS-通过控制器有条件路由时在新选项卡中打开链接,javascript,angularjs,tabs,angularjs-ng-click,Javascript,Angularjs,Tabs,Angularjs Ng Click,在我的应用程序中,我有两种类型的组织简介。当用户单击某个配置文件名称时,我首先需要检查我是否有该组织的高级配置文件,如果有,请将用户发送到该方向。如果没有高级配置文件,用户将被发送到标准配置文件 我使用ng click来实现这一点,它向控制器发送id和资源(在本例中为组织)参数,在控制器中评估条件,然后将用户路由到正确的方向。当用户正常单击时,所有这些都正常工作 但是,当用户试图在新选项卡中打开链接时,通过右键单击并选择该选项,新选项卡将打开当前页面的url。因此,ng click and co

在我的应用程序中,我有两种类型的组织简介。当用户单击某个配置文件名称时,我首先需要检查我是否有该组织的高级配置文件,如果有,请将用户发送到该方向。如果没有高级配置文件,用户将被发送到标准配置文件

我使用ng click来实现这一点,它向控制器发送id和资源(在本例中为组织)参数,在控制器中评估条件,然后将用户路由到正确的方向。当用户正常单击时,所有这些都正常工作

但是,当用户试图在新选项卡中打开链接时,通过右键单击并选择该选项,新选项卡将打开当前页面的url。因此,ng click and controller在打开新选项卡之前没有触发或评估请求

如何通过代码进行更改,以便Angular在打开新选项卡之前处理ng click请求?或者更广泛地说,我如何允许我的用户在新选项卡中打开其中一个链接,从而使它们不只是显示在他们当前所在的页面上

HTML


我知道这是个老问题。但是我找到了一个解决办法&把它贴在这里可能会帮助其他人

首先,我为右键单击创建了一个自定义指令:

module.directive('tabRightClick',['$parse', function($parse) {
    return function(scope, element, attrs) {
        var fn = $parse(attrs.tabRightClick);
        element.bind('contextmenu', function(event) {
            scope.$apply(function() {
    //            event.preventDefault();
                fn(scope, {$event:event});
            });
        });
    };
}]);
然后将此指令作为HTML文件中的属性应用,并调用在
ng click上调用的相同方法:

<div ng-controller="ProfileRouter">
        <div ng-repeat="org in orgs | orderBy:'org.name'">
            <a href="{{locationPath}}" ng-click="profileCheck( '{{ org.id }}', 'organisation' )" tab-right-click= "profileCheck( '{{ org.id }}', 'organisation' )">{{ org.name }}</a>
        </div>
</div>
希望对你有帮助

module.directive('tabRightClick',['$parse', function($parse) {
    return function(scope, element, attrs) {
        var fn = $parse(attrs.tabRightClick);
        element.bind('contextmenu', function(event) {
            scope.$apply(function() {
    //            event.preventDefault();
                fn(scope, {$event:event});
            });
        });
    };
}]);
<div ng-controller="ProfileRouter">
        <div ng-repeat="org in orgs | orderBy:'org.name'">
            <a href="{{locationPath}}" ng-click="profileCheck( '{{ org.id }}', 'organisation' )" tab-right-click= "profileCheck( '{{ org.id }}', 'organisation' )">{{ org.name }}</a>
        </div>
</div>
$scope.profileCheck = function (id, resource) {

    $http({method: 'GET', url:'/IdCheck', params:{'id': id})
    .success(function(data) {

        var count = data.hits.found;
        if (count) {
            var hash = data.hits.hit[0].id;
        }

       if (resource == 'organisation') {
            theResource = 'universities';
            page = 'overview';
        }

        if (count == 1) {
            window.location.href = "/" + theResource + "/profile/" + hash + "/" + page;
            $scope.locationPath = "/" + theResource + "/profile/" + hash + "/" + page;
        }
        if (count == 0) {
            window.location.href = "/" + resource + "/" + id;  
            $scope.locationPath = "/" + resource + "/" + id;
        }

     })
    .error(function(data) {
        $scope.data = data || "Can't get resource";
    });  
}