Question

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.

Était-ce utile?

La solution

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!');
           }
        }
    });
}])
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top