如何使用 AngularJS 获取刚刚添加到 Firebase 的新列表项的 UID? [英] How do I get the UID of the new list item I just added to Firebase using AngularJS?
本文介绍了如何使用 AngularJS 获取刚刚添加到 Firebase 的新列表项的 UID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在向 Firebase 列表添加新子项,并且会自动创建唯一 ID.我需要某种方法来获取创建的 ID,以便我可以立即将 URL 位置路径更改为包含该 ID 的路由.这是我创建列表项的方式:
I am adding new children to a Firebase list and unique IDs are automatically created. I need some way to get the ID thats been created so I can immediately change the URL location path to a route containing that ID. Here is how I am creating the list items:
.controller('NewTestCtrl', ['$scope', 'syncData', '$location', function($scope, syncData, $location) {
$scope.newTitle = null;
$scope.tests = syncData('tests');
$scope.createTest = function() {
var newDate = new Date();
if( $scope.newTitle ) {
$scope.tests.$add({
title: $scope.newTitle,
createdDate: newDate,
modified: newDate,
user: $scope.auth.user.uid
});
$location.path("/test/" + [FIREBASE UNIQUE ID] + "/1");
}
};
}])
感谢您的帮助!
推荐答案
angularFire $add 方法返回一个 promise,当它被解析时,它将包含一个 Firebase 对您新推送的值的引用.使用该值,您可以获得 UID.
The angularFire $add method returns a promise, which when resolved will contain a Firebase reference to your newly pushed value. With that value, you can get the UID.
$scope.tests.$add({
...your object....
})
.then(function(ref) {
$location.path("/test/"+ref.name());
});
这篇关于如何使用 AngularJS 获取刚刚添加到 Firebase 的新列表项的 UID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文