mrkibzk mrkibzk - 5 months ago 52
AngularJS Question

ng-if ng-options in AngularJS

I would like to filter my data by some id. When I chose team, I want to filter players in select from that team.

This is my HTML

<div class="col-sm-2" style="width:14.666667%">
<select style="width: 135px;" ng-model="selectedTeam" ng-options="x.team_name for x in teams" ng-change="changeLocationTeam(selectedTeam.team_id)">
<option value="" selected disabled>SELECT TEAM</option>
<div class="col-sm-2" ng-if="showPlayer()" style="width:14.666667%">
<select style="width: 150px;" ng-model="selectedPlayer" ng-options="y.first_name + ' ' + y.last_name for y in players" ng-change="changeLocationPlayer(selectedPlayer.team_id,selectedPlayer.player_id)">
<option value="" selected disabled>SELECT PLAYER</option>
<div ng-if="showReset()">
<a ui-sref="activity-statistic.activity-statistic-club-heart" style="font-size:7px;">RESET TO TEAM VIEW</a>

Any idea?

Answer Source

In order to achieve that you have to know to which team the player belongs. I assume you have that information in player objects with a property team_id. Then you have just to filter your options by team_id.

ng-options="y.first_name + ' ' + y.last_name for y in players | filter: {team_id:selectedTeam.team_id}

Here's a working fiddle.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download