2014-09-25 27 views
0

我有這樣的數據:http://www.monde-du-rat.fr/API/moulinette/radio/posts.json,從CakePHP的應用程序,它的歌曲按歌曲,附票計數值

我把它作爲一個服務爲一體的angularjs應用程序,我展示它像這樣的HTML:

  <div ng-repeat="song in songs | orderBy:'Radio.idSong' | notesong:'Radiovote'" class="list-group-item" id="{{song.Radio.idSong}}" ng-class="{ 'active' : songPlayed_name == song.Radio.name }" ng-if="songs"> 
       <span>{{song.Radio.idSong}} - {{song.Radio.title}}</span><br /> 
       <span>{{note}}%</span> 
      </div> 

所以,我要統計每個連接的投票,並與值「好」或「壞」的定義,喜歡

的%

我嘗試做這個過濾器:

/* notesong */ 
app.filter('notesong', function() { 
    return function(input) { 

    // counter init 
    var countGood = 0; 

    // if there is no votes, so note is zero 
    if (!angular.isArray(input)) { 
     var note = 0; 
    } else { 
     // loop for each vote (from Radiovote array, each value) 
     angular.forEach(input, function() { 
      if (input.value == 'good') { 
       countGood = countGood + 1; 
      } 
     }); 
     var note = (countGood * input.length)/100; 
    } 

    // final return 
    return note; 

    }; 
}); 

它顯然不工作(沒有錯誤,也沒有數據顯示),那麼,什麼是正確的方法?

回答

1

您正在將過濾器應用於錯誤的地方。而不是使用它的ng-repeat的,你應該使用它要綁定,這樣的屬性:

<div ng-repeat="song in songs | orderBy:'Radio.idSong'" class="list-group-item" id="{{song.Radio.idSong}}" ng-class="{ 'active' : songPlayed_name == song.Radio.name }" ng-if="songs"> 
    <span>{{song.Radio.idSong}} - {{song.Radio.title}}</span><br /> 
    <span>{{song.Radiovote | notesong}}%</span> 
</div> 

還有與你的循環中票的過濾器的方式有問題。更新以下行:

// loop for each vote (from Radiovote array, each value) 
angular.forEach(input, function (item) { 
    if (item.value == 'good') { 
    countGood = countGood + 1; 
    } 
}); 
+0

THKS :)但每個歌曲的音符爲'0' – JoDiii 2014-09-25 13:56:44

+0

您的過濾器也有問題。我已經更新了我的答案。請檢查它是否有幫助。 – bmleite 2014-09-25 14:59:31

+0

THKS :)一切都很好 – JoDiii 2014-09-25 15:54:16