Why doesn’t the data appear on the screen using firebase and angular?

Asked

Viewed 292 times

2

Good afternoon, you guys, Something that should be simple and I can’t. I’m checking my database at firebase, where you have two records stored. I do the search and appear returns the data in the console, using console.log(), HOWEVER, I can not display this data on the screen, where it should appear!!!

app.controller('ListagemCtrl', function($scope, $rootScope, $location, $firebaseObject){
$rootScope.activetab = $location.path();

$scope.filmesCadastrados = [];

firebase.database().ref('filmes/').once('value').then(function (snapshot) {
    for(var id in snapshot.val()){

        var filme = snapshot.val()[id];
        console.log(filme);

        $scope.filmesCadastrados.push(filme);
    }
});

});

And this is my HTML:

<div ng-controller="ListagemCtrl">
  <table class="table table-striped" >
    <tbody>
        <tr> 
            <th>Título do Filme</th>
            <th>Diretor</th>
            <th>Categoria</th>
            <th>Duração</th>
        </tr>
        <tr ng-repeat="filme in filmesCadastrados">
            <td>{{filme.titulo}}</td>
            <td>{{filme.diretor}}</td>
            <td>{{filme.categoria}}</td>
            <td>{{filme.duracao}}</td>
        </tr>
    </tbody>
    </table>
</div>

This displays on the console: inserir a descrição da imagem aqui Where am I going wrong?

  • Show me what’s on console.log please

  • Shows the output of your console.log

  • I just posted what the console displays.

  • Object {category: "FIC", director: "Jon Favreau", duration: 130, title: "Iron Man"}category: "FIC"director: "Jon Favreau"duration: 130title: "Iron Man"proto: Object controllerr.js:38 Object {category: "FIC", director: "George Lucas", duration: 150, title: "Star Wars"}

  • @Gustavosevero where you put the tag ng-app in your HTML?

  • in <html ng-app="app">

  • Fix your post, you have 2 ng-controller

  • I fixed it, thanks.

Show 3 more comments

2 answers

1

The update, triggered by a Promise firebase, is not firing the cycle $digest Angular. Use the service $timeout, that provides a wrapper that invokes the cycle, to provoke the delay by the Angular:

app.controller('ListagemCtrl', function($scope, $rootScope, $timeout, $location, $firebaseObject){
  $rootScope.activetab = $location.path();

  $scope.filmesCadastrados = [];

  firebase.database().ref('filmes/').once('value').then(function (snapshot) {

    $timeout(function(snapshot) {

      for(var id in snapshot.val()){

        var filme = snapshot.val()[id];
        console.log(filme);

        $scope.filmesCadastrados.push(filme);
      };

    }, 1);
  });
});
  • Thanks for the tip, Onosendai, but I already got it... I don’t know how, but I got it. Another thing, I tried to use your code, up there, but when I rotate, on the console it appears " 'val' Undefined " and points to the line pain FOR.

  • @Gustavosevero I’m glad to know that your code is working. When at val(), it is worth checking its implementation - I just indicated how an isolation would work in the $timeout wrapper.

  • My implementation is the one in the post. snapshot.val() is the reference of the data that is there in firebase. If you put Snapshop.val()[id], as shown in a row below, it picks up the id of that data there in firebse

1


Do it:

$scope.filmesCadastrados.push({
    titulo: filme.titulo,
    diretor: filme.diretor,
    categoria: filme.categoria,
    duracao: filme.duracao
})

Browser other questions tagged

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