2014-12-02 30 views
0

我正在研究來自電視api的Json數據。從這個API我想使用「tvshow.episode.ratings.loved」值。我怎樣才能將這個值鏈接到像 $ scope.like = tvshow.episode.ratings.loved;我想用這個做一些數學運算。我如何將這個冗長的json文本轉換爲$ scope.like?我應該在哪裏包括它在我的代碼?請幫助如何將json數據項轉換爲變量?

var app = angular.module('MyApp',[]); 

app.controller("mainController", function ($scope, $http, $filter){ 

    $scope.filteredItems = [];  

    $scope.pagedItems = []; 
    $scope.curPage = 1; 

    $scope.init = function() { 

     $http.jsonp('http://api.trakt.tv/calendar/premieres.json/' + $scope.apiKey + '/' + apiDate + '/' + 30 + '/?callback=JSON_CALLBACK').success(function(data) { 

      angular.forEach(data, function(value, index){ 


       angular.forEach(value.episodes, function(tvshow, index){ 
        tvshow.date = date; 
        $scope.results.push(tvshow); 
        angular.forEach(tvshow.show.genres, function(genre, index){ 
         //Only add to the availableGenres array if it doesn't already exist 
         var exists = false; 
         angular.forEach($scope.availableGenres, function(avGenre, index){ 
          if (avGenre == genre) { 
           exists = true; 
          } 
         }); 

        }); 

       }); 
      });     
      }; 


     }).error(function(error) { 

     }); 
    }; 

    app.$inject = ($scope, $filter); 



}); 
+0

我爲了幫助您正確,我們需要知道什麼是JSON的樣子;) – jValdron 2014-12-02 14:30:21

+0

http://api.trakt.tv /calendar/premieres.json/982db1c71a81fffd5d1a582b094c07b2 – 2014-12-02 14:32:18

+0

PS。你通常不想公開發布你的API密鑰。 – jValdron 2014-12-02 14:34:33

回答

3

$scope.groupedItems = []; 

添加

$scope.loved = []; 

而且在這個循環中添加

angular.forEach(value.episodes, function(tvshow, index) { 
     // .... code 
    $scope.loved.push(tvshow.ratings.loved); 
}) 

如果你只想展示評級爲每個項目,你需要添加在你模板這個

<span class="label"><i class="icon-thumbs-up"></i> {{tvshow.show.ratings.loved}}</span> 
<span class="label"><i class="icon-thumbs-down"></i> {{tvshow.show.ratings.hated}}</span> 

在結果你得到陣列,所有的「愛的評級爲」