我创建了一个小应用程序。我使用了实时数据库。首先,我使用一个查询从数据库中获取数据。当一条记录添加到数据库时。我可以拿到这些数据。但我必须将新数据附加到现有数据。我怎样才能追加?
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}];
发布于 2019-04-22 01:12:06
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();
});
});
https://stackoverflow.com/questions/55784884
复制相似问题