Hermes
Hermes

Reputation: 462

AngularJs add new data to ng-repeat

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}];

Upvotes: 0

Views: 125

Answers (1)

Umesh
Umesh

Reputation: 971

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();   
    });
});

Upvotes: 1

Related Questions