How to use Angular Factory with firebase?

Asked

Viewed 120 times

0

Good afternoon, Guys, I’m trying to create a Factory with firebase, to bring the data from it.

My controller:

app.controller('ListagemCtrl', function($scope, $location, $firebaseObject, $timeout, appFactory){

$scope.filmesCadastrados = [];

appFactory.pegaFilmes().on('value', function (snapshot) {
    for(var id in snapshot.val()){
        var filme = snapshot.val()[id];
        $scope.filmesCadastrados.push({
            titulo: filme.titulo,
            diretor: filme.diretor,
            categoria: filme.categoria,
            duracao: filme.duracao
        });
    }
});
});

And this is my Factory:

app.factory('appFactory', function($scope, $firebaseObject){

var _pegaFilmes = function() {
    return firebase.database().ref('filmes/').on('value', function (snapshot) {
                for(var id in snapshot.val()){
                    var filme = snapshot.val()[id];
                    //console.log(filme);
                    $scope.filmesCadastrados.push({
                        titulo: filme.titulo,
                        diretor: filme.diretor,
                        categoria: filme.categoria,
                        duracao: filme.duracao
                    });

                }
            });
}

return {
    pegaFilmes: _pegaFilmes
};

});

This warning appears on the console:

angular.js:13708 Error: [$injector:unpr] http://errors.angularjs.org/1.5.7/$injector/unpr? P0=copeProvider%20%3C-%20%24scope%20%3C-%20appFactory

1 answer

1


Then you take a look at the dock https://www.firebase.com/docs/web/libraries/angular/api.html#angularfire

  app.factory('Filmes', function($firebaseArray){
      var chatRef = firebase.database().ref('/filmes');

     return $firebaseArray(chatRef);
  });

  app.controller('FilmeCtrl', function($scope, Filmes){
    $scope.filmes =  [];

    // Function auto executada
    (function () {
      console.log(Filmes);
      Filmes.$loaded(success, error);

      function success(data) {
        $scope.filmes =  data;
        console.log('MIUUII', data);
      }

      function error(errorMessage) {
        console.log(errorMessage);
      }
    })();
  });
  • I’m gonna try, thanks.

  • What a show!!! It worked!! It was worth a lot

  • Paulo, how do I get a property from Firebase data? Type, I want to take one of the elements of the json object, example, just the title of the movie.

  • That’s all you need, I’ve got it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.