我試圖通過websockets添加新記錄時動態更新視圖。最新的記錄(默認情況下它的ID最高)應該在底部,但是它會隨機添加。我無法找到它發生的原因。
angular.module('Persistence').controller("PersistenceController", [
"$scope",
"$http",
"$filter",
"$timeout",
"$mdDialog",
"$sce",
function ($scope, $http, $filter, $timeout, $mdDialog, $sce) {
$scope.records = [];
$scope.sortById = 'id';
var socket2 = io.connect('192.168.1.100:3001');
socket2.on('newInformation', function (data) {
$scope.records.push({
title: data.data.title,
id : data.data.id,
name: data.data.name
});
$scope.$apply();
});
}
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Persistence" ng-controller="PersistenceController">
<div ng-repeat="record in records | orderBy : sortById">
<div>{{record.id}}</div>
<div>{{record.title}}</div>
<div>{{record.name}}</div>
</div>
</body>
您的變量名稱錯誤。在記錄對象中用'id'改變'title'的位置。 –
@BurakAkyıldız謝謝,我改變了它,但仍然沒有效果。 – salep