Are complex expressions possible in ng-hide / ng-show?

前端 未结 5 1043
难免孤独
难免孤独 2020-12-07 16:04

I want to do so:

ng-hide=\"!globals.isAdmin && mapping.is_default\"

but the expression evaluates always to false.

相关标签:
5条回答
  • 2020-12-07 16:42

    I generally try to avoid expressions with ng-show and ng-hide as they were designed as booleans, not conditionals. If I need both conditional and boolean logic, I prefer to put in the conditional logic using ng-if as the first check, then add in an additional check for the boolean logic with ng-show and ng-hide

    Howerver, if you want to use a conditional for ng-show or ng-hide, here is a link with some examples: Conditional Display using ng-if, ng-show, ng-hide, ng-include, ng-switch

    0 讨论(0)
  • 2020-12-07 16:42

    Some of these above answers didn't work for me but this did. Just in case someone else has the same issue.

    ng-show="column != 'vendorid' && column !='billingMonth'"
    
    0 讨论(0)
  • 2020-12-07 16:47

    Use a controller method if you need to run arbitrary JavaScript code, or you could define a filter that returned true or false.

    I just tested (should have done that first), and something like ng-show="!a && b" worked as expected.

    0 讨论(0)
  • 2020-12-07 16:57

    ng-show / ng-hide accepts only boolean values.

    For complex expressions it is good to use controller and scope to avoid complications.

    Below one will work (It is not very complex expression)

    ng-show="User=='admin' || User=='teacher'"
    

    Here element will be shown in UI when any of the two condition return true (OR operation).

    Like this you can use any expressions.

    0 讨论(0)
  • 2020-12-07 16:59

    This will work if you do not have too many expressions.

    Example: ng-show="form.type === 'Limited Company' || form.type === 'Limited Partnership'"

    For any more expressions than this use a controller.

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