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;
};
});
它顯然不工作(沒有錯誤,也沒有數據顯示),那麼,什麼是正確的方法?
THKS :)但每個歌曲的音符爲'0' – JoDiii 2014-09-25 13:56:44
您的過濾器也有問題。我已經更新了我的答案。請檢查它是否有幫助。 – bmleite 2014-09-25 14:59:31
THKS :)一切都很好 – JoDiii 2014-09-25 15:54:16