Ng-show / hide - based on a substring in a string

I am familiar with using ng-show and ng-hide when my anchor data contains a specific word or piece of text. For instance:

<div ng-show="myArray.Length > 20">
    Show something
</div>

<div ng-show="myData.Name == 'Harry Roberts'">
    Show something
</div>

However, how can I use ng-showto display when the associated data contains a specific value, such as "Current". For example, if my data is JSON:

{
    "MyAddresses": [
        {
            "Entry"  : "1",
            "Period" : "2011 - current",

        }, {
            "Entry"  : "2",
            "Period" : "2003 - 2011",

        }, {
            "Entry"  : "3",
            "Period" : "1998 - 2001",

        }
    ]
}


<div ng-show="myData.MyAddresses.Period ~ 'Current'">
    Show something
</div>
+4
source share
2 answers

Use the function indexOfto search for a substring in a string. It will return the position of the search string if it is found or -1 is returned.

This way you can use a type expression myData.MyAddresses.Period.indexOf('Current') != -1to show / hide data

+8

<div ng-show = "address.indexOf('expected string')!=-1"> Show somethingโ€‹ </div>

+1

Source: https://habr.com/ru/post/1544869/


All Articles