how to compare a string in angular
27,243
Solution 1
Here is one way to to do in pure Javascript:
['research','doctor','educator'].each(function(val){
if (val == 'doctor') {
console.log("I'm a doctor");
}
});
Or with filter:
['research','doctor','educator'].filter(function(val){
if (val == 'doctor') {
return val;
}
});
You can use a custom filter, where you can apply the same logic as described in my answer. Here is a very nice article about this: https://scotch.io/tutorials/building-custom-angularjs-filters.
So you pass a parameter as a filter value to your custom filter function, then in your html markup you apply the filter function.
Solution 2
In html, I used filter, have a good day.
<span ng-if="(fields | filter: 'doctor': true )[0]">
do something ...
</span>
Author by
Jasmine
Updated on May 25, 2020Comments
-
Jasmine almost 4 years
I have an array and I use ng-repeat to display the items in the array. However, I want to compare if the array contains the word "doctor". And if it contains doctor, I need to do some logic.
My array is as follows
fields : ['research','doctor','educator']
How do I do it?