【问题标题】:make href url dynamic with angular with two same href link to a single href link使href url动态与角度与两个相同的href链接到单个href链接
【发布时间】:2019-01-11 17:38:35
【问题描述】:

下面是我的 html 代码,我需要的 href 应该是基于我当前状态的动态的,而不是使用两个不同的 div,我可以只使用一个 div

<div ng-if="ctrl.currentState == 'employee'">
    <small><a href="/createQuote" title="Change Settings"></a></small>
</div>
<div ng-if="ctrl.currentState == 'fleetmanager'">
        <small><a href="/createQuote/fleetmanager" title="Change Settings"></a></small>
</div>

【问题讨论】:

    标签: html angularjs


    【解决方案1】:

    您可以使用ngHref

    <small><a ng-href="{{myLink}}" title="Change Settings"></a></small>
    

    在你的控制器中

    $scope.myLink = $scope.currentState == 'employee' ? '/createQuote' : '/createQuote/fleetmanager';
    

    这种方式好一点,因为这部分也可以用单元测试覆盖。

    但是如果你想把这个逻辑放到模板中,check out类似的问题

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2016-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-06
      • 2014-04-10
      • 1970-01-01
      相关资源
      最近更新 更多