How can I use ng-if to check if a string contains other substrings?

First, I want to use ng-if to check if the string contains other substrings "#teamA" or "#teamB", for example:

<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 doesn't work. Second, I want each {{msg}} to have a different class assigned (teamA and teamB). Am I doing this correctly (if my works are ng-if)?

+3


source to share


2 answers


According to your requirement, you need to use 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>

      

+7


source


You have double quotes inside a double quote. Your code is working fine.



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

      

+5


source







All Articles