【问题标题】:AngularJs if statement in ng-hrefng-href 中的 AngularJs if 语句
【发布时间】:2015-07-05 11:39:50
【问题描述】:

我有一些动态创建的元素,每个元素都有不同的 ng-href。

我想根据一些元素给出不同的链接。

当我尝试在 ng-href 中编写函数时,它会将页面发送到 url 中的函数,因此它不起作用。

我尝试做这样的事情;

       .......
             <a 
            ng-href='if(m){#linkOne} else {#linkTwo}'
            ng-click='test(type);'>

              </a> 
       .......

我应该使用哪种方法来创建具有不同 ng-href 的元素?

【问题讨论】:

    标签: javascript angularjs model-view-controller


    【解决方案1】:

    这样做很简单。

    <a href="{{variable == value ? '#link1' : '#link2'}}">Link</a>
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

      <a ng-href="{{m ? '#linkOne':'#linkTwo'}}" ng-click="test(type)">your link</a>
      

      http://jsfiddle.net/54ema4k0/

      【讨论】:

        【解决方案3】:

        你需要为链接使用一个变量

        <a ng-href='{{link}}' ng-click='test(type);'> your link </a>
        

        然后

        $scope.$watch('m', function(value){
            $scope.link = value ? 'link1': 'link2';
        })
        

        演示:Fiddle

        【讨论】:

          【解决方案4】:

          试试条件运算符

          <a ng-href='m ? "link1" : "link2" ' ng-click='test(type);'> your link </a>
          

          在范围内设置您的链接值,

          在您的控制器中,

          $scope.dynamic_url = 'somelink'
          

          在视图中,

          <a ng-href='dynamic_url' ng-click='test(type);'> your link </a>
          

          【讨论】:

          • 条件运算符不起作用,它发送到" m ? "link1" : "link2" " in url
          • 您在哪里声明和定义 m 范围?
          猜你喜欢
          • 1970-01-01
          • 2014-12-12
          • 2014-03-14
          • 2012-09-21
          • 1970-01-01
          • 1970-01-01
          • 2015-07-05
          • 2015-02-16
          • 2013-03-26
          相关资源
          最近更新 更多