简体   繁体   English

如何在 ng 上调用 function-单击<a>能够在新选项卡中打开的链接</a>

[英]How to call function on ng-click on a <a> link with the ability to open in new tab

I have an element which goes something like this我有一个类似这样的元素

<a ng-click="vm.openPage(page)">{{page.pageId}}</a>

There some validations in the vm.openPage() function which are necessary before it redirects to the page with this code vm.openPage() function 中有一些验证,在重定向到带有此代码的页面之前是必需的

$state.go('page', {
   id: page.pageId
});

The problem is that I need that link to be able to be opened in a new tab with right-click but still run the function and the validation.问题是我需要能够通过右键单击在新选项卡中打开该链接,但仍运行 function 和验证。 Tried it with试过了

ng-href={{vm.openPage(page)}}

but that runs the function at page load, and I can't have that because the link is a column in a table with 20 rows.但是在页面加载时运行 function ,我不能这样做,因为链接是一个有 20 行的表中的列。

Is there a way to achieve this?有没有办法做到这一点?

use the following:使用以下内容:

In your html:在您的 html 中:

 <a ng-href="{{(test() && url && !loading)||'#'}}">click here</a>

in your JS:在你的 JS 中:

var url = "http://www.google.com";
$scope.test = function () {

     if(  $scope.pass =="pass") { // your condition
        return true;
     }
     else {
       return false;
     }

   };

make sure that while loading your page, the condition is false if it is true, set add loading status flag to your load function, and use it in your link确保在加载页面时,如果条件为真,则条件为假,将添加加载状态标志设置为加载 function,并在链接中使用它

sample example in the following jsFiddle link以下 jsFiddle链接中的示例示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM