0

I need your help, i have controller like this:

var apps = angular.module('test',['firebase']);

function app(query, $scope) {
  $scope.result = query.data('marendra79@yahoo.com')
}

and the factory like this

apps.factory('query', function($firebase) {
  FIREBASE_URL = "https://tokolaundry.firebaseio.com/"
  var ref = new Firebase(FIREBASE_URL+'laundry');
  var laundry = $firebase(ref);

  return {
    data: function(email) {
      var result = null;
      var query = $firebase(ref.startAt(email).endAt(email));
      query.$on('loaded', function (value) {
        if (value != null) {
          var key = query.$getIndex()[0];
          this.result = key;
          console.log(this.result)
        }

        return result;
      });
    }
  }
});

The question why i still get null value in $scope.result but i get the key in console log. how i can pass the key value to $scope.result in controller? thanks

runTarm
  • 11,537
  • 1
  • 37
  • 37
marendra
  • 51
  • 1
  • 7
  • Your problem is very similar to the one here: http://stackoverflow.com/questions/25024882/create-new-directory-and-inserting-data-in-firebase/25025844#25025844. The cause is unrelated to Firebase. A extremely well rated non-Firebase answer that covers the topic is here: http://stackoverflow.com/questions/111102/how-do-javascript-closures-work?rq=1. I prefer this explanation, although it ranks slightly lower: http://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example?rq=1 – Frank van Puffelen Jul 30 '14 at 11:03
  • As of the 0.8 release, $on has been removed. You can use $asArray() or $asObject() to synchronize your data. – David East Jul 31 '14 at 20:49

0 Answers0