2015-04-04 80 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