设置自己的密钥时将对象添加到火力地堡 - AngularFire [英] Setting Your Own Key When Adding an Object to Firebase - AngularFire
问题描述
我可以使用下面的一个对象添加到我的火力地堡数据存储:
I can use the following to add an object to my Firebase data store:
var uniqueId = {
name: "a name",
location: "new york"
}
$scope.myItems.$add(uniqueId).then(function(firebaseId){
// do something on success
}, function(){
// do something if call fails
});
以上将添加一个对象到我的数据存储,如果添加成功,则返回由火力地堡生成的ID。我刚添加的对象此项下保存。
The above will add an object into my data store and if the add is successful, an ID generated by Firebase is returned. The object I just added is saved under this key.
有没有办法让我指定密钥的名字是什么,当我添加到我的数据存储?
Is there a way for me to specify what the key name is when I add to my data store?
推荐答案
以下面的网址,例如
https://myapp.firebaseio.com/users/
比方说,我们要创建一个在这个位置的1键作为一个孩子的用户。我们的网址是这样的。
Let's say we want to create a user with a key of 1 as a child at this location. Our URL would look like this.
https://myapp.firebaseio.com/users/1
要创建一个使用AngularFire我们可以创建在用户节点的引用,并调用$孩子(1)创建到该位置的引用的用户。
To create a user using AngularFire we can create a reference at the users node and call $child(1) to create a reference to that location.
var usersRef = new Firebase('https://myapp.firebaseio.com/users');
var userRef = new Firebase('https://myapp.firebaseio.com/user/1');
$scope.users = $firebase(usersRef);
// these are the same
$scope.userOne = $firebase(userRef);
$scope.userOne = $scope.users.$child(1);
然后我们可以使用 $设置
给用户的值存储在该位置。
Then we can use $set
to store the value of the user at that location.
var usersRef = new Firebase('https://myapp.firebaseio.com/users');
$scope.users = $firebase(usersRef);
$scope.users.$child(1).set({
first: 'Vincent',
last: 'Van Gough',
ears: 1
});
在你的情况将是:
var uniqueId = {
id: 1,
name: "a name",
location: "new york"
};
$scope.myItems.$child(uniqueId.id).$set(uniqueId);
请记住,使用 $设置
将在该位置销毁任何previous数据。非破坏性更新的值,用 $更新
。
Remember that using $set
will destroy any previous data at that location. To non-destructively update the values use $update
.
这篇关于设置自己的密钥时将对象添加到火力地堡 - AngularFire的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!