2016-07-27 73 views
1

我試圖在我的角度應用程序中實現一個選擇,以顯示有關表格中電視節目劇集的數據。ng選項在選擇中不起作用

在選擇中,用戶應該能夠選擇其中一個劇集。

我嘗試了ng-repeat選項元素,它的工作。但是當我嘗試爲選擇設置ng-options時,它們似乎不被識別。

任何想法,我在這裏錯了什麼?

這裏是我的html:

<div class="container-fluid full-height"> 
    <div id="tabs_container" class="col-xs-12"> 


     <div ng-app="seriesAnalyzer" ng-controller="configTableController"> 
      <label> 
       Episode: 
       <select> 
        <option ng-repeat="episode in all_episodes | orderBy:['season_number','episode_number']">Season: {{episode.season_number}} Episode: {{episode.episode_number}}</option> 
       </select> 
      </label> 

      <!-- This is not working --> 
      <label> 
       Episode: 
       <select ng-options="episode.season_number for episode in all_episodes track by id"> 
        <option value="">Please select an episode...</option> 
       </select> 
      </label> 

      <div ng-init="episode = episode_data"> 
       <p>{{episode_data.episode_number}}</p> 
       <p>{{episode_data.season_number}}</p> 
      </div> 

      <table st-table="rowCollection" class="table table-striped"> 
       <thead></thead> 
       <tbody> 
       <tr ng-repeat="scene in episode_data.configuration_matrix"> 
        <td ng-repeat="values in scene track by $index" ng-style="set_color(values)" >{{values}}</td> 
       </tr> 
       </tbody> 
      </table> 

     </div> 

     <br> 
    </div> 
</div> 

而且我的控制器:

seriesAnalyzer.controller('configTableController', function ($scope, $http) { 

var req_url = 'http://localhost:8080/api/episodes'; 
$http.get(req_url) 
    .success(function (data) { 
     $scope.all_episodes = data; 
     console.log($scope.all_episodes); 
    }) 
    .error(function (data) { 
     console.log('Error: ' + data); 
    }); 


$scope.update_season_number = function(){ 
    console.log(val); 
}; 

$scope.update_episode_number = function(){ 
    console.log("Input changed"); 
}; 

var get_episode_data = function() 
{ 
    var req_url = 'http://localhost:8080/api/episodes?season_id=' + $scope.season_number + '&episode_id=' + $scope.episode_number 
    $http.get(req_url) 
     .success(function (data) { 
      $scope.episode_data = data; 
     }) 
     .error(function (data) { 
      console.log('Error: ' + data); 
     }); 
}; 

.... 

回答

3

嘗試:

<select ng-options="episode as episode.season_number for episode in all_episodes track by episode.id" ng-model="selectedEpisode"> 
    <option value="">Please select an episode...</option> 
</select> 

更多信息可以在ng-options documentation

而且在這裏找到是example jsfiddle以及(採用了棱角分明1.5.1)

而且track by episode.id可能不適合您的角度版本的工作,所以你可能需要刪除它

在你需要兩個賽季數和集數,然後做的情況下如下:

<select ng-options="episode.season_number + ' ' + episode.episode_number for episode in all_episodes track by episode.id" ng-model="selectedEpisode"> 
    <option value="">Please select an episode...</option> 
</select> 
+0

這適用於我,但我需要season_number和episode_number顯示爲「季節1插曲2」。我怎樣才能做到這一點? – Igle

+0

@Igle' episode.season_number +''+ episode.episode_number for episode in all_episodes track by episode.id' – Akis

+0

@Igle you can also'group by'' episode.season_number' – Akis