Javascript 使用来自服务器的数据刷新ng repeat
好的,我有以下架构: Laravel4动力RESTAPI AngularJS电动前端 我使用ng repeat在对象数组上循环。我还有一个表单,其中一个填写了一定数量的数据,而Laravel则填写了剩余的特定时间戳。因为直到服务器端才定义新数据的最终形式,所以我必须返回它并将其添加到范围中的对象数组中 这是我的控制器: 现在,在页面重新加载期间检索数据不是问题。然而,我遇到的障碍是在创建新游戏数据时。基本上这个过程是这样的: 用户填写表单并提交 现在我尝试使用$scope.$apply,但这导致了一个错误Javascript 使用来自服务器的数据刷新ng repeat,javascript,php,arrays,angularjs,Javascript,Php,Arrays,Angularjs,好的,我有以下架构: Laravel4动力RESTAPI AngularJS电动前端 我使用ng repeat在对象数组上循环。我还有一个表单,其中一个填写了一定数量的数据,而Laravel则填写了剩余的特定时间戳。因为直到服务器端才定义新数据的最终形式,所以我必须返回它并将其添加到范围中的对象数组中 这是我的控制器: 现在,在页面重新加载期间检索数据不是问题。然而,我遇到的障碍是在创建新游戏数据时。基本上这个过程是这样的: 用户填写表单并提交 现在我尝试使用$scope.$apply,但这导致
那么,如何使ng repeat正确刷新表呢 当您使用$scope.$apply时,是否可以发布您遇到的错误?它说:$digest已经在进行中尝试包装一个持续时间为0的$timeout,它应该自动处理$apply错误free@AlexandrinRus您是否有示例要展示?是否希望在服务器数据更改时自动运行getGames?也许你可以使用酒吧/酒吧服务,我就是这么做的。
app.controller('GameCtrl', function ($scope, RestService) {
$scope.games = [];
$scope.newGame = {secretcode: ''};
//debug
window.scope = $scope;
$scope.getGames = function () {
RestService.get('games')
.success(function (response) {
$scope.games = response.data;
})
.error(function (response, statuscode) {
$scope.flash = 'An error ' + statuscode + ' occurred.';
});
};
$scope.createNewGame = function () {
RestService.post('games', $scope.newGame)
.success(function (response) {
$scope.games.push(response.game);
})
.error(function (response) {
console.log(response);
});
};
$scope.getGames();
});
`-> angularjs sends the data to server
-> laravel 4 adds some new data (timestamps mostly) and creates a new database entry
-> laravel 4 returns the newly created data back to angularjs
-> angularjs adds the new entry into the array of objects
-> angularjs refreshes the table generated from the array since there is a new entry`