'Allow both upper case and lower case inside ng-if
I have condition like this
<table ng-if="name=='john' && name=='MARK'">
//rest of the code
</table>
My code will execute when ng-if is true.
Now my doubt is , ng-if should be true even when name is in both upper and lower case..
<table ng-if="name=='JOhN' && name=='maRK'">
//rest of the code
</table>
Is it possible?
I have tried this using if-else condition also.
if(name.toString()='john'){
alert('success')
}
else{
alert('failure')
}
Is it correct?
Solution 1:[1]
try,
<table ng-if="name.toLowerCase()=='john' && name.toLowerCase()=='mark'">
//rest of the code
</table>
or with angular lowercase filter
<table ng-if="(name| lowercase) =='john' && (name | lowercase)=='mark'">
//rest of the code
</table>
Solution 2:[2]
I know this has been answered and accetped but I would do it with a function and then return the result of checking the name given with an array of accepted names. This way - it is easier to change the names in the future or to add other names as required.
//html
<table ng-if="checkName(name)">
//rest of the code
</table>
//js
checkName(name){
let acceptedNames = ['john','mark'];
let nameStr = name.lowerCase();
let result = false;
if(acceptedNames.indexOf(nameStr) > -1 ){result = true};
return result;
}
Solution 3:[3]
Better way to do is to convert both values to same case using the filter | uppercase) and do comparision
Solution 4:[4]
Use the lowercase or uppercase pipe to match the condition
ng-if="name.toLowerCase()=='john'"
This is the easiest way to acheive the ng if condition
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Azad |
| Solution 2 | |
| Solution 3 | Sajeetharan |
| Solution 4 | Surya R Praveen |
