AngularFire 推送到数组不起作用
AngularFire pushing to array doesn't work
我正在开发一个联系人簿应用程序,它可以包含包含成员的群组。成员有姓名、头衔和电子邮件,但这并不重要。我有以下控制器,其中包含所有组的 array
,以及我当前正在处理的组的索引。此外,还有一个函数可以将成员添加到特定组,如下所示:
groupControllers.controller('MemberGroupController', ['$scope', '$firebaseArray', '$routeParams', '$location', '$routeParams', function($scope, $firebaseArray, $routeParams, $location, $routeParams) {
$scope.groups = $firebaseArray(ref);
$scope.whichGroup = $routeParams.groupId;
this.addToGroup = function(member){
$scope.groups[$scope.whichGroup].members.push(member);
$scope.groups[$scope.whichGroup].members.$save();
};
}]);
我知道 addToGroup 函数正在获取成员,因为它将正确的成员记录到控制台。出于某种原因,当我这样做时,它实际上并没有将成员添加到 firebase
。谢谢!
您只需在 $firebaseObject
个实例上调用 $save
。事实上,调用 $save
可能会破坏事情,因为它会触发 push
和 $save
之间的潜在竞争条件。
更新
这会起作用:
groupControllers.controller('MemberGroupController', ['$scope', '$firebaseArray', '$routeParams', '$location', '$routeParams', function($scope, $firebaseArray, $routeParams, $location, $routeParams) {
$scope.groups = $firebaseArray(ref);
$scope.whichGroup = $routeParams.groupId;
this.addToGroup = function(member){
ref.child('groups').child($scope.whichGroup).child('members').push(member);
};
}]);
避免筑巢
请注意,您似乎正在构建一个所谓的 "nest",一个看似直观但会限制您的层次结构。阅读 Firebase documentation on structuring data 部分以了解更多信息。
考虑将群组成员与其其他元数据分开:
groups
-Jy32724372
name: "Group 1"
-Jy46728671
name: "Group 2"
groups_members:
-Jy32724372
twitter:3247423678
email:23478101
anonymous:23054389
-Jy46728671
email:23478101
anonymous:45764357
调用 firebase 对象的 $save 方法将本地更改推送到 firebase。查看以下 AngularFire $save 指南:
https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html#section-api-summary
查看 $save 方法指南。希望对您有帮助!!!
我正在开发一个联系人簿应用程序,它可以包含包含成员的群组。成员有姓名、头衔和电子邮件,但这并不重要。我有以下控制器,其中包含所有组的 array
,以及我当前正在处理的组的索引。此外,还有一个函数可以将成员添加到特定组,如下所示:
groupControllers.controller('MemberGroupController', ['$scope', '$firebaseArray', '$routeParams', '$location', '$routeParams', function($scope, $firebaseArray, $routeParams, $location, $routeParams) {
$scope.groups = $firebaseArray(ref);
$scope.whichGroup = $routeParams.groupId;
this.addToGroup = function(member){
$scope.groups[$scope.whichGroup].members.push(member);
$scope.groups[$scope.whichGroup].members.$save();
};
}]);
我知道 addToGroup 函数正在获取成员,因为它将正确的成员记录到控制台。出于某种原因,当我这样做时,它实际上并没有将成员添加到 firebase
。谢谢!
您只需在 $firebaseObject
个实例上调用 $save
。事实上,调用 $save
可能会破坏事情,因为它会触发 push
和 $save
之间的潜在竞争条件。
更新
这会起作用:
groupControllers.controller('MemberGroupController', ['$scope', '$firebaseArray', '$routeParams', '$location', '$routeParams', function($scope, $firebaseArray, $routeParams, $location, $routeParams) {
$scope.groups = $firebaseArray(ref);
$scope.whichGroup = $routeParams.groupId;
this.addToGroup = function(member){
ref.child('groups').child($scope.whichGroup).child('members').push(member);
};
}]);
避免筑巢
请注意,您似乎正在构建一个所谓的 "nest",一个看似直观但会限制您的层次结构。阅读 Firebase documentation on structuring data 部分以了解更多信息。
考虑将群组成员与其其他元数据分开:
groups
-Jy32724372
name: "Group 1"
-Jy46728671
name: "Group 2"
groups_members:
-Jy32724372
twitter:3247423678
email:23478101
anonymous:23054389
-Jy46728671
email:23478101
anonymous:45764357
调用 firebase 对象的 $save 方法将本地更改推送到 firebase。查看以下 AngularFire $save 指南: https://www.firebase.com/docs/web/libraries/angular/guide/synchronized-objects.html#section-api-summary
查看 $save 方法指南。希望对您有帮助!!!