1

I am working with some data via Firebase, and for some reason when the value changes on the server, the scope is getting updated in my console, however the angular view is not updating.

I've tried some different approaches including wrapping the reference to the scope in a $timeout & $apply, but still no solution. I've worked with angular many times and I've never ran into this behavior before. There's only one controller in the app.

js

app.controller('someCtrl', function($scope, $timeout){

    $scope.publishedItems =[];
    var fbRef = firebase.database().ref('stories/' + storyId + "/correspondents").orderByChild('published').equalTo(true);
    fbRef.on('value', function(snapshot) {
    //update scope

    $scope.publishedItems = snapshot.val();

    console.log($scope.publishedItems)// this logs the correct data, however the markup is not changing

});

html

 <div ng-controller="someCtrl">
      <div ng-repeat="item in publishedItems">
        {{item}}
      </div>
 </doc>

resolved

So after some debugging, it turned out I had a function running outside of the Angular app that was appending some markup to the dom which was interfering with Angulars digest cycle.

Working as expected now

3
  • can you please create codepen.io example? and $scope.$apply migh help you Commented Oct 26, 2017 at 12:21
  • @ShirishPatel I added a snippet although I didn't expose my firebase ref. Commented Oct 26, 2017 at 12:36
  • you can use $watchCollection() on the publishedItems, and it will start watching the each changes on the array. Commented Oct 26, 2017 at 12:52

3 Answers 3

2

Assuming snapshot.val() returns something you need to trigger a digest cycle to determine changes since firebase callback is executed outside angular

fbRef.on('value', function(snapshot) {
    $scope.$apply(function(){
      $scope.publishedItems = snapshot.val();
    })
})
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, yup, I've tried that and same result. Still is not updating in the view.
that's wierd, you definitely need a call to $apply, try posting a link to a plunkr with the complete code
Yeh, never experienced this. Yeh Im gonna have to create a plunkr with another firebase instance.
of course I created a plunked and it worked. The only main diffferebxe between the plunkr is the data is being manipulated from another source....would that make a difference?
so I just rebooted and everything worked as expected.. weird
|
0

You can try following code

var app = angular.module('myApp', []);
app.controller('someCtrl', function ($scope, $timeout) {

    $scope.publishedItems = [];
    $scope.$watchCollection('publishedItems', function(newCol, oldCol, scope) {
        console.log(newCol, oldCol, scope);
    });
    var fbRef = firebase.database().ref('stories/' + storyId + "/correspondents").orderByChild('published').equalTo(true);
    fbRef.on('value', function (snapshot) {
        var pubItems = snapshot.val();
        //update scope

        $scope.publishedItems = pubItems;

        console.log($scope.publishedItems)// this logs the correct    data, however the markup is not changing

    });
});

Comments

0

Seems like you need to update you binding , so you need to use $scope.$apply()

app.controller('someCtrl', function($scope, $timeout){

    $scope.publishedItems =[];
    var fbRef = firebase.database().ref('stories/' + storyId + "/correspondents").orderByChild('published').equalTo(true);
    fbRef.on('value', function(snapshot) {
    //update scope

    $scope.publishedItems = snapshot.val();



    console.log($scope.publishedItems)// this logs the correct data, however the markup is not changing

});
   $scope.$apply();
});

4 Comments

Hi, yup, I've tried that and same result. Still is not updating in the view.
I added a snippet although i didnt expose my firebase ref.. but you can at least see where I'm having the issue.
@gregdevs you are missing a } at the end of your script
Thanks, fixed that..although still having this issue.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.