0

I want to do some simple string comparisons, calculations and filtering on my firebase data INSIDE MY CONTROLLER before I send it to the view. I can't seem to figure out how to access the data inside the firebase object.

here is my firebase object within 'tests/'+$routeParams.id:

{
   "createdDate":"2014-03-30T18:04:28.927Z",
   "modified":"2014-03-30T18:04:28.927Z",
   "region":"shoulder",
   "step1":{
      "defined_incident":"no",
      "localized_or_referred":"localized",
      "recent_surgery":"no",
      "symptoms":"constant"
   },
   "title":"testtest",
   "user":"simplelogin:1"
}

here is my controller:

.controller('TestCtrl', ['$scope', 'syncData', '$routeParams', function($scope, syncData, $routeParams) {

    syncData('tests/'+$routeParams.id).$bind($scope,'test');

    //this is where i want to access $scope.test 
    //and check if $scope.test.region is equal to 'shoulder', etc.

    if($scope.test.region.$value == 'shoulder') {
        if($scope.test.step1.localized_or_referred.$value == 'localized') {
            alert('Shoulder & Localized!');
        }
    }
}])

What am I missing here? I have also tried converting the firebase object to an array using the OrderByPriority filter, but without success.

1 Answer 1

1

Your issue is that your code which uses the value of $scope.test is called way too soon after $bind. You need to give you application time to sync with Firebase, until that point you wont have any data.

The $bind method returns a promise which will be resolved once the initial data is loaded from Firebase. Try something like this:

.controller('TestCtrl', ['$scope', 'syncData', '$routeParams', function($scope, syncData, $routeParams) {
    syncData('tests/'+$routeParams.id).$bind($scope,'test').then(function () {
        //this is where i want to access $scope.test 
        //and check if $scope.test.region is equal to 'shoulder', etc.
        if($scope.test.region.$value == 'shoulder') {
            if($scope.test.step1.localized_or_referred.$value == 'localized') {
               alert('Shoulder & Localized!');
           }
        }
    });
}])
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I knew it was something simple. It works perfectly.
I'm new to angular and firebase. What does syncData do? Is it part of either library?

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.