Angular.js ng-switch ---testing if value is empty or undefined

前端 未结 3 2115
无人及你
无人及你 2021-02-12 11:48

In Angular.js templating, I need to test if a value is undefined or empty.... I am not sure how to do this since ng-switch-when tests for expressions in strings. I

相关标签:
3条回答
  • 2021-02-12 12:02

    Following solution would also work (using angular 1.3.10):

    <div ng-switch="selection">
        <div ng-switch-default>The string is not empty!</div>
        <div ng-switch-when="undefined">The string is empty!</div>
    </div>
    
    0 讨论(0)
  • 2021-02-12 12:10
    <div ng-switch="!!selection">
       <div ng-switch-when="false">Empty, Null or undefined</div>
       <div ng-switch-when="true">The string is not empty</div>
    </div>
    
    0 讨论(0)
  • 2021-02-12 12:23

    ng-switch allows expressions, you can use placeholder, as such:

    <div ng-switch="selection || '_undefined_'" >      
          <span ng-switch-when="_undefined_">I am set to zero or undefined?!</span>      
          <span ng-switch-default>This string is not empty</span>
    </div>
    

    For more info: ng-switch on empty string

    You usually do it within controller(see answer#1 in linked thread).

    0 讨论(0)
提交回复
热议问题