AngularJS 打开 Link if 条件

AngularJS open Link if condition

我想检查 AngularJS 中 href 内的 if 条件:

<a class="button button-block" href="if(cond){#/app/abc}else{nothing}" ng-click="doSomething()">Suchen</a>

其中 cond 是可以为真或为假的条件,并且 link 仅在满足此条件时才为开放式。

有谁知道这是否可行,如果可行,如何实现?

你可以有类似下面的东西,当 cond 为真时,它会创建一个带有 URL 的 href,否则它会创建一个空白的 href,它不会重定向到任何地方.

ng-href="{{cond? '#/app/abc': ''}}"

否则请从锚标记中删除 href,并在 $location.path('#/app/abc') 的帮助下处理 ng-clickdoSomething 函数本身的重定向逻辑OR $state.go(取决于您使用的路由器引擎)用于重定向。

要使用 ng-if 获得预期结果,您可以使用以下选项

<div ng-if="cond" ng-show="cond">
  <a class="button button-block" href="#/app/abc" ng-click="doSomething()" ng-model="cond">Suchen</a>
</div>
<a class="button button-block" href="nothing" ng-click="doSomething()" ng-hide="cond" ng-model="cond">abc</a>

JS:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.cond='sai';// comment to see abc link with href= nothing and uncomment to show href with mentioned path
});

为了区别,我用了不同的名字- Suchen 和 abc

Codepen- http://codepen.io/nagasai/pen/NrRoay