Search code examples
ruby-on-railsangularjsslim-lang

if else if else in angular js


I have to incorporate angular in my slim file, I'm not sure how to transform if else block in angular. I know angular don't have ng if else statements but is there a way to change below code to angular

- if @cart['empty'] 
   cart is empty
- elsif @cart['invalid']
   can't proceed
- else
  -@cart['Items'].each do |item|
      #{item['description']}

I want to achieve some thing like this

  ng-if="cart.empty"
     cart is empty

  ng-else-if cart.invalid
     can't proceed

  ng-else
     ng-repeat="cart.Items as item"
          item.description

Solution

  • What you're looking for is the ng-switch directive.

    First get some variable that will contain the switch condition:

    $scope.getStatus = function(cart) {
      if (cart.empty) return 'empty';
      if (cart.invalid) return 'invalid';
    }
    

    And then use the directive:

    <div ng-switch = "getStatus(cart)">
      <div ng-switch-when = "empty">Cart is empty</div>
      <div ng-switch-when = "invalid">Can't proceed</div>
      <div ng-switch-default>.... ok ....</div>
    </div>