1

I an new in angularjs and I have json in my angularjs application, I want ng-switch to work when that json is not empty(it is an array). it's a large json and I'm talking about one part of it,is it possible with ng-switch? or I should set scope parameter in my controller? or some other ways that I do not know of

JSON

...,
"venues": [{"vid":"12",...},{"vid":"13",},{"vid":"14",..}],...

Markup

<div ng-switch on="team.venues" >
    <ul ng-switch-when="venue is not empty">
        <li ng-repeat="venue in team.venues">
            {{venue.vid}}
        </li>
    </ul>
    <p ng-s­wit­ch-­def­ault>no venue</p>
</div>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299
Shirin Abdolahi
  • 1,047
  • 8
  • 18

2 Answers2

2

yes use like this

<div ng-switch on="team.venues.length > 0" >
            <ul ng-switch-when="true">
                <li ng-repeat="venue in team.venues">
                 {{venue.vid}}
                </li>
            </ul>
            <p ng-s­wit­ch-­def­ault>no venue</p>
</div>

this will show the <ul ng-switch-when="true"> when team.venues.length > 0 is true.

this is what DOC says,

<ANY ng-switch="expression">
    <ANY ng-switch-when="matchValue1">...</ANY>
    <ANY ng-switch-when="matchValue2">...</ANY>
    <ANY ng-switch-default>...</ANY>
</ANY>

here is a official DEMO

Kalhan.Toress
  • 21,683
  • 8
  • 68
  • 92
1

You could directly use direct expression there like ng-switch-when="team.venues.length > 0 && team.venues".

Markup

<div ng-switch on="team.venues">
     <ul ng-switch-when="team.venues.length > 0 && team.venues">
       <li ng-repeat="venue in team.venues">
          {{venue.vid}}
       </li>
     </ul>
     <p ng-s­wit­ch-­def­ault>no venue</p>
</div>
Pankaj Parkar
  • 134,766
  • 23
  • 234
  • 299