How to use ng-if to test if a variable is defined

后端 未结 3 1255
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 23:24

Is there a way to use ng-if to test if a variable is defined, not just if it\'s truthy?

In the example below (live demo), the HTML displays a shipping cost

相关标签:
3条回答
  • 2021-02-04 23:47

    I edited your plunker to include ABOS's solution.

    <body ng-controller="MainCtrl">
        <ul ng-repeat='item in items'>
          <li ng-if='item.color'>The color is {{item.color}}</li>
          <li ng-if='item.shipping !== undefined'>The shipping cost is {{item.shipping}}</li>
        </ul>
      </body>
    

    plunkerFork

    0 讨论(0)
  • 2021-02-04 23:47

    You can still use angular.isDefined()

    You just need to set

    $rootScope.angular = angular;
    

    in the "run" phase.

    See update plunkr: http://plnkr.co/edit/h4ET5dJt3e12MUAXy1mS?p=preview

    0 讨论(0)
  • 2021-02-04 23:59

    Try this:

    item.shipping!==undefined
    
    0 讨论(0)
提交回复
热议问题