1

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

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

This idea makes sense. But if rails controller returns error, like @error consists an error message and no cart variable,. How can I add that condition (if error ) in switch block.
@tumati assuming you have the error in your scope, give the error to the function ng-switch = "getStatus(cart, error)" and getStatus will return 'error' and that's a new ng-switch` statement. Now that you know about ng-switch you can developp any approach, we cannot do everything for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.