Pregunta

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");
     }
  };
}])

Thanks for your help!

¿Fue útil?

Solución

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()); 
  });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top