2015-04-04 132 views
0

我一直在試圖讓角度js爲我訂購我的數據。但唉,它不起作用角度不訂購數據

我從firebase(https://boiling-inferno-4886.firebaseio.com/champion)檢索數據並使用節點js將它發送到控制器。

控制器代碼

var myApp = angular.module('myApp', []); 
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) { 


// Pulls list of games 
$scope.pullGame = function() { 
    console.log("Here"); 
    $http.get('/getGameData').success(function(response) { 
    console.log(response); 
    $scope.championlist = response; 
    }); 

}; 
}]); 

然後即時顯示使用所述信息NG-重複

<!DOCTYPE> 
<html ng-app="myApp"> 
<head> 
    <!-- Latest compiled and minified CSS --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
<!-- Optional theme --> 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css"> 
<title>DevCha</title> 

</head> 
<body> 
<div class="container" ng-controller="AppCtrl"> 

<input class="form-control" ng-model="game.name"> 
<button class="btn btn-info" ng-click="pullGame()">Update</button> 

<h1>DevCha</h1> 

<table class="table"> 
    <thead > 
    <tr> 
     <th>Champion Name</th> 
     <th>Wins</th> 
     <th>Losses</th> 
    </tr> 
    </thead> 
    <tbody> 
    <tr ng-repeat="champion in championlist|orderBy: 'name'"> 
     <td>{{champion.name}}</td> 
     <td>{{champion.wins}}</td> 
     <td>{{champion.losses}}</td> 
    </tr> 
    </tbody> 
</table> 
</div> 


<script src="controllers/controller.js"></script> 

</body> 
</html> 

我已用火力對數據進行排序嘗試,但不工作或者(這是bizarr) 。但是,我很高興,如果任何將整理對我來說(角或火力)

節點JS代碼,該數據推送到角

app.get('/getGameData', function(req, res){ 

    var ref = new Firebase("https://boiling-inferno-4886.firebaseio.com/champion"); 

    ref.once("value", function(snapshot) { 
     // Success 
     console.log(JSON.stringify(snapshot.val())); 
     res.json(snapshot.val()); 

    }, function (errorObject) { 
     console.log("The read failed: " + errorObject.code); 
     //callback("Failure", null); 
    }); 

}); 

SAMPLE_DATA - 1:{名稱:「吉姆」,榮獲:10,損失:5},2:{name:'Fred',Win:8,Losses:5} (實際數據可以在firebase鏈接中找到)

tl:dr我需要訂購我的數據, Firebase內置的方法對其進行排序,並以角度|進行排序orderBy但都不工作

+0

究竟是什麼工作?你有錯誤嗎?另外,我建議嘗試使用數據的本地副本來查看'orderBy'是否有效。 – 2015-04-04 05:24:46

+0

你可以粘貼樣本數據嗎? – 2015-04-04 05:25:22

+0

數據位於firebase的鏈接上。沒有錯誤,數據完美顯示,但沒有按我指定的名稱(名稱)排序。 – jromaior 2015-04-04 05:28:12

回答

0

我們真的需要看看這些數據,但是因爲我之前有過這個問題,所以這裏是我的答案。

正如您在documentation中看到的那樣,orderBy僅命令數組。 (鍵,值)語法適用於對象。也許在數組中轉換你的字典會有所斬獲。

如果沒有,你可以寫一個過濾器來爲你做。看看這個過濾器:

app.filter('orderObjectBy', function() { 
    return function (items, field, reverse) { 
    var filtered = []; 
    angular.forEach(items, function (item) { 
     filtered.push(item); 
    }); 
    filtered.sort(function (a, b) { 
     return (a[field] > b[field] ? 1 : -1); 
    }); 
    if (reverse) filtered.reverse(); 
    return filtered; 
    }; 
}); 

那麼你可以使用它像這樣:

<div class="item" ng-repeat="item in items | orderObjectBy:'position'"> 

</div> 

的方式,你可以改變的順序是這樣的:

<ul> 
    <li ng-repeat="item in items | orderObjectBy:'color':true">{{ item.color }</li> 
</ul> 

假的上升和true is descending