[英]AngularJS doesn't push new record into the array
I create a new record, this record is successfully added to the database. 我创建一个新记录,此记录已成功添加到数据库中。 That's good.
那很好。 But the problem is that it doesn't update the list of existing records - means the
push
function doesn't seems to be working. 但是问题在于它不会更新现有记录的列表-意味着
push
函数似乎无法正常工作。
This is inside the controller: 这在控制器内部:
$scope.posts = Posts.query();
...
$scope.saveForm = function(post) {
var p = new Post(post);
p.$save(function (response) {
$scope.posts.push(angular.extend(p, response.id));
console.log('saved');
console.log(response.id);
});
};
I've tried to use only push()
without angular.extend()
, also tried to add $scope.posts = Posts.query();
我试图只使用
push()
而没有angular.extend()
,也试图添加$scope.posts = Posts.query();
in the end of the controller, but it didn't help - the new record is still not added to the current list of posts. 在控制器的末尾,但没有帮助-新记录仍未添加到当前帖子列表中。
What might cause this? 是什么原因造成的? I've found similar topics here on SO, but it didn't help me to solve this issue.
我在SO上找到了类似的主题,但这并没有帮助我解决这个问题。
Thank you. 谢谢。
EDIT: I see in console.log($scope.posts);
编辑:我在
console.log($scope.posts);
看到 the new post, it just doesn't appear in the list of posts. 新帖子,只是没有出现在帖子列表中。
EDIT2: I tried also $scope.$apply();
EDIT2:我也试过
$scope.$apply();
after the push
command, but it led to this error message: 在
push
命令之后,但导致此错误消息:
Error: [$rootScope:inprog] $digest already in progress
EDIT3: I found out that when I try to delete a records, the list doesn't get updated as well (only after refresh). EDIT3:我发现当我尝试删除记录时,列表也不会得到更新(仅在刷新后)。
EDIT4: When I add a new record, in the JS is this output (the new record, properly saved, just not updated in the list where are all items): EDIT4:当我添加新记录时,在JS中是以下输出(正确保存了新记录,只是未在所有项目的列表中更新):
Resource {id: Object, title: "hohohohooho", body: null, starred: null, created_at: "2015-10-27T04:00:06.943Z"…}
ID
is accessible as response.id.$oid
. 可通过
response.id.$oid
访问ID
。
This works for me... 这对我有用...
p.$save().then(function(response){
$scope.posts.push(response.id)
});
Angular extend doesn't save the list/array. 角扩展不保存列表/数组。 It extends the old list/array with new item.
它用新项目扩展了旧列表/数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.