Reputation: 3
I'm trying to display all the equipment list that i retrieve from the firebase database using angular.foreach but when ever i try to display the retrieved values in my view it only displays the last value. Need help!
My JS
/*global angular*/
var app = angular.module('sdt', ['ngRoute', 'firebase']);
app.config(['$routeProvider', function ($routeProvider) {
'use strict';
$routeProvider.when('/sdt', {
templateUrl: 'searchdowntime/sdt.html',
controller: 'sdtCtrl'
});
}]);
app.controller('sdtCtrl', ['$scope', '$firebaseObject', '$firebaseArray', function ($scope, $firebaseObject, $firebaseArray) {
'use strict';
var ref = firebase.database().ref();
var data = ref.child("data");
var list = $firebaseArray(data);
list.$loaded().then(function(data) {
$scope.data = data;
angular.forEach ($scope.data , function (d) {
angular.forEach (d.equipments, function (e) {
$scope.allEquipments = e;
console.log($scope.allEquipments);
})
});
console.log($scope.data);
}).catch(function(error) {
$scope.error = error;
});
}]);
My html
<div class="table-responsive">
<table class="table table-striped table-hover">
<tr>
<th id="system">System</th>
<th id="equipment">Equipment</th>
<th id="date">Date</th>
<th id="type">Type</th>
</tr>
<tr data-ng-repeat="d in allEquipments">
<td headers = "system">System</td>
<td headers = "equipment" >{{d}}</td>
<td headers = "date">date</td>
<td headers = "type">Standby</td>
</tr>
</table>
console log:
view:
Upvotes: 0
Views: 236
Reputation: 5077
That's because you always changing the value of $scope.allEquipments
for every iteration. You need to push it into an array instead.
app.controller('sdtCtrl', ['$scope', '$firebaseObject', '$firebaseArray', function ($scope, $firebaseObject, $firebaseArray) {
'use strict';
// Add this line
$scope.allEquipments = [];
var ref = firebase.database().ref();
var data = ref.child("data");
var list = $firebaseArray(data);
list.$loaded().then(function(data) {
$scope.data = data;
angular.forEach ($scope.data , function (d) {
angular.forEach (d.equipments, function (e) {
// Change this line
$scope.allEquipments.push(e);
console.log($scope.allEquipments);
})
});
console.log($scope.data);
}).catch(function(error) {
$scope.error = error;
});
}]);
And your ng-repeat
to
<tr data-ng-repeat="d in allEquipments">
<td headers = "system">System</td>
<td headers = "equipment" >{{d.equipment}}</td>
<td headers = "date">date</td>
<td headers = "type">Standby</td>
</tr>
Upvotes: 1