最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - ng-showhide - based on a substring in a string - Stack Overflow

programmeradmin5浏览0评论

I'm familiar with using ng-show and ng-hide when my bound data contains a specific word or piece of text. For example:

<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-show to show when the bound data contains a certain value, for example, 'Current'. For example, if my JSON data:

{
    "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>

I'm familiar with using ng-show and ng-hide when my bound data contains a specific word or piece of text. For example:

<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-show to show when the bound data contains a certain value, for example, 'Current'. For example, if my JSON data:

{
    "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>
Share Improve this question edited Jun 17, 2014 at 9:34 guru 4,0221 gold badge30 silver badges33 bronze badges asked Jun 17, 2014 at 9:03 Oam PsyOam Psy 8,66135 gold badges96 silver badges166 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 12

Use the function indexOf to search for a substring in a string. It will return the position of the search string if found or will return -1.

So you can use an expression like myData.MyAddresses.Period.indexOf('Current') != -1 to show/hide data

Below code works for me to check the substring

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

发布评论

评论列表(0)

  1. 暂无评论