Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng if with angular for string contains

I have the following Angular code:

<li ng-repeat="select in Items">             <foo ng-repeat="newin select.values"> {{new.label}}</foo> 

How can I use an ng-if condition to look for a specific character:

ng-if="select.name == '?'"  

to only display the code when the character is here? The value I have is like 88?77 and the numbers are dynamic but the question mark is always there, I can't seem to filter based on that?

like image 402
Poiro Avatar asked Jun 16 '15 13:06

Poiro


People also ask

How do you compare strings in Ng-if?

ngIf expression example and === and == operator is used to compare the strings and returns Boolean value.

How do you check if a string contains a substring in angular HTML?

ES2015 have String#includes method that checks whether a string contains another. This can be used if the target environment supports it. The method returns true if the needle is found in haystack else returns false .

How do you check if a string contains a substring JS?

To check if a substring is contained in a JavaScript string:Call the indexOf method on the string, passing it the substring as a parameter - string. indexOf(substring) Conditionally check if the returned value is not equal to -1. If the returned value is not equal to -1 , the string contains the substring.


2 Answers

ES2015 UPDATE

ES2015 have String#includes method that checks whether a string contains another. This can be used if the target environment supports it. The method returns true if the needle is found in haystack else returns false.

ng-if="haystack.includes(needle)" 

Here, needle is the string that is to be searched in haystack.

See Browser Compatibility table from MDN. Note that this is not supported by IE and Opera. In this case polyfill can be used.


You can use String#indexOf to get the index of the needle in haystack.

  1. If the needle is not present in the haystack -1 is returned.
  2. If needle is present at the beginning of the haystack 0 is returned.
  3. Else the index at which needle is, is returned.

The index can be compared with -1 to check whether needle is found in haystack.

ng-if="haystack.indexOf(needle) > -1"  

For Angular(2+)

*ngIf="haystack.includes(needle)" 
like image 81
Tushar Avatar answered Sep 28 '22 09:09

Tushar


ng-if="select.name.indexOf('?') !== -1"  
like image 37
Mathew Berg Avatar answered Sep 28 '22 09:09

Mathew Berg