how do I use $id to be number or new var in Firebase Angularjs?

so I have this $scope to call a table in Firebase named Questions

$scope.questions=$firebaseArray(fireBaseData.refQuestions());

and I call the database this way

{{questions}}

it shows exactly like this

[{"nama":"Perkataan yang saya sampaikan dapat diingat dan berkesan untuk orang lain.",
    "nilai":"item7",
    "$id":"1",
    "$priority":null
},
{"nama":"Saya sering mengucap “terimakasih” atas pertolongan orang lain agar terlihat ramah.",
    "nilai":"item6",
    "$id":"2", // THIS VALUE THAT I NEEDED
    "$priority":null
}]

QUESTIONS:

what I am looking for is to call just $id value like 2 to be new var or $scope so I cant make them to be question number

because I need new var or $scope such

$scope.questionNo = $firebaseArray(fireBaseData.refQuestions().$id);

$scope.question =;
$scope.totalQuestions = $scope.questions.length;
$scope.currentRoute = $scope.questionNo;

but failed

Upvotes: 2

Views: 63

Answers (1)

Velko Ivanov
Velko Ivanov

Reputation: 83

Your question requires some clarification, but since StackOverflow won't let me post a comment, here it goes ...

If I understand you correctly, you are trying to access a particular question by it's $id, so you need to do the following:

$scope.questions = $firebaseArray(fireBaseData.refQuestions().orderByChild('$id').equalTo('2'));
$scope.question = $scope.questions[0];

You can check angularfire docs and firebase docs on filtering.

However this situation will benefit mostly from a different structure of your database, as explained here. It will be a lot easier to access questions by their $id if it was the key under which they were stored, in which case you could simply do

$scope.question = $firebaseObject(fireBaseData.refQuestions().child('2'));

Upvotes: 1

Related Questions