3

First, I want to use ng-if to check if the string contains others substring "#teamA" or "#teamB" like:

<ion-item ng-if="msg.indexOf("#teamA") > -1" class="teamA">            
    {{msg}}
</ion-item>
<ion-item ng-if="msg.indexOf("#teamB") > -1" class="teamB">            
    {{msg}}
</ion-item>

This code is not working. Secondly, I want each {{msg}} to have different assigned class (teamA and teamB). Am I on the right way to do that (if my ng-if works)?

2 Answers 2

7

Per you requirement, what you need to use actually is ngClass

ng-class="{'teamA': msg.indexOf('#teamA') > -1, 'teamB': msg.indexOf('#teamB') > -1}" 

Then you only need one element

<ion-item ng-class="see above...">            
{{msg}}
</ion-item>
2
  • It works like a charm. Thanks. Does it work with any javascript expression in ng-class?
    – cnjap
    Commented May 25, 2015 at 2:42
  • Yes all javascript expression can work here but what you need to keep in mind is all objects in this expression should be able to access in current scope or they are globally defined.
    – Rebornix
    Commented May 25, 2015 at 2:46
5

You have double quotes inside of a double quote. Your code works just fine.

<div ng-if="msg.indexOf('#teamA') > -1" class="teamA">
    {{ msg }}
</div>
<div ng-if="msg.indexOf('#teamB') > -1" class="teamB">
    {{ msg }}
</div>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.