0

I created a small application. I used realtime database. Firstly I used a query to get data from database. When a record added to database. I can get that data. But I have to append new data to existing data. How can I append?

HTML

<tr ng-repeat="i in result track by $index">
    <td>{{i.id}}</td>
    <td>{{i.category}}</td>
    <td>{{i.status}}</td>       
</tr>

JS

var app = angular.module('socket', []); 
app.controller('homeController', function ($scope) {        
    var socket = io.connect('http://localhost:3000');
    socket.on('list', function (data) {     
        $scope.result =  data;
        if (!$scope.$$phase) $scope.$apply();   
    });
});

Data

First query result

data = [
        {id:1, category:2, status:1},
        {id:2, category:1, status:2}
       ];

New data result

newData = [{id:3, category:1, status:2}];

1 Answer 1

1
Try this:

var app = angular.module('socket', []); 
app.controller('homeController', function ($scope) {        
    var socket = io.connect('http://localhost:3000');
    $scope.result = []
    socket.on('list', function (data) {     
        $scope.result = $scope.result.concat(data);
        if (!$scope.$$phase) $scope.$apply();   
    });
});
Sign up to request clarification or add additional context in comments.

2 Comments

Concat is working. But order doesn't work. I want to see last data on the top
@Hermes $scope.result = data.concat($scope.result) will do the trick

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.