Firebase binding not being reflected in Angular view

前端 未结 2 391
春和景丽
春和景丽 2021-01-21 10:05

I am pulling out one value from every object in Firebase, picUrl (the url of a picture) and storing that in a scope array variable, $scope.bricks. How do I make it so $scope.br

2条回答
  •  逝去的感伤
    2021-01-21 10:24

    As I said in my comment, you can probably get it working by:

        firebaseObj.$loaded().then(function(){
            angular.forEach(firebaseObj.products, function(value, key){
                $scope.bricks.push({src: value.picUrl});
                $scopy.$apply();
            });
        });
    

    Update: the above doesn't work, see OP's comment below.

    To learn more about $scope.$apply read this article: http://jimhoskins.com/2012/12/17/angularjs-and-apply.html

    But even though this may fix your current issue, you'll run into other problems later. The reason for this is that you're creating an "unmanaged array" of bricks. AngularFire has quite some code to ensure that Firebase's order collections and AngularJS's two-way data-bound arrays play nice together.

    For this reason it is probably better if you set up a separate sync for your array of products:

    angular.module('noorApp').controller('MainCtrl', function ($scope, $http, $firebase) {
        var sync = $firebase(ref);
        var firebaseObj = sync.$asObject();
        $scope.bricks = $firebase(ref.child('products')).$asArray();    
    });
    

    With this setup, AngularFire will call $apply() automaticaly when the array items initially load or are modified afterwards.

提交回复
热议问题