html - My loop is only displaying the last data value in my view - firebase -
i'm trying display equipment list retrieve firebase database using angular.foreach when ever try display retrieved values in view displays 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:
that's because changing value of $scope.allequipments
every iteration. need push array instead.
app.controller('sdtctrl', ['$scope', '$firebaseobject', '$firebasearray', function ($scope, $firebaseobject, $firebasearray) { 'use strict'; // add 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 line $scope.allequipments.push(e); console.log($scope.allequipments); }) }); console.log($scope.data); }).catch(function(error) { $scope.error = error; }); }]);
and 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>
Comments
Post a Comment