I am using angularJS with a node back-end that sends data using socketio. Then I try to show the data using ng-repeat. If I initialize the data inside the controller, then ng-repeat works fine, but if I add data when it is received from the server, nothing will be displayed on the screen when ng-repeat is displayed.
Here is the code snippet I'm using: (Trans.html)
<div id='trans' ng-app='MyApp'>
<h1>Transactions</h1>
<div class="pure-g-r" ng-controller='TransController'>
<div class="pure-u-1-2" ng-repeat="trans in transactions">
<h1>{{ trans.name }}</h1>
<p>{{ trans.description}}</p>
<p>{{ trans.date }}</p>
<p>{{ trans.category }}</p>
</div>
</div>
</div>
<script src="/js/loadAngular.js"></script>
(controller.js)
function TransController($scope, socket) {
$scope.transactions = [];
$scope.currentMonth = "March";
$scope.categories = [];
init();
function init() {
console.log("emitting init functs");
socket.emit('data', {request: 'transactions', month: $scope.currentMonth});
socket.emit('getCategories', {});
};
socket.on('dataResponse', function (data) {
console.log(data);
if(data.response === 'transactions'){
$scope.transactions = [];
var tran = data.data;
for(var i = 0; i < tran.length; i++){
$scope.transactions.push(tran[i]);
console.log(tran[i]);
};
}
console.log($scope.transactions);
console.log("CURRENT MONTH " + $scope.currentMonth);
});
(server.js)
var myTransactions = {
January : [{
name: "Tesco shopping",
date: "January",
description: "This is my description, I went to the shop and bought loads of food.",
category: "Food"
}],
February : [],
March : [{
name: "Booze shopping",
date: "March",
description: "This is my description, I went to the shop and bought loads of food.",
category: "Food"
},{
name: "Tesco shopping",
date: "March",
description: "This is my description, I went to the shop and bought loads of food.",
category: "Food"
}],
...
};
mudev.addCustomEvent('data', function(socket,data){
var request = data.request;
var month = data.month;
console.log("Data Request: request:",request, " month:",month);
if(month==null){
socket.emit('dataResponse', {
response: 'transactions',
data: myTransactions
});
}else{
socket.emit('dataResponse', {
response: 'transactions',
data: myTransactions[month]
})
}
});
The only difference that I see between the data is that when I statically initialize the data inside the controller, there is an additional key called "HashKey", which is absent when the data is sent from the server. I read the angular documentation again, but can't find anything about this problem.
.
( , ng-repeat )