如何在Firebase Angularjs中使用$id作为数字或新的var ?

时间:2020-12-24 23:59:22

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

我有这个$scope来调用Firebase中的一个名为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

我所要寻找的是只调用$id值2作为新的var或$scope,所以我不能让它们成为问题号

because I need new var or $scope such

因为我需要新的var或$scope。

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

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

but failed

但失败了

1 个解决方案

#1


1  

Your question requires some clarification, but since * 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:

如果我理解正确,您试图通过它的$id访问一个特定的问题,所以您需要执行以下操作:

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

You can check angularfire docs and firebase docs on filtering.

您可以查看angularfire文档和firebase文档的过滤。

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

但是,这种情况将主要受益于数据库的不同结构,如本文所述。如果问题是存储在其中的键,那么通过他们的$id访问问题就会容易得多,在这种情况下,您可以简单地这样做

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

#1


1  

Your question requires some clarification, but since * 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:

如果我理解正确,您试图通过它的$id访问一个特定的问题,所以您需要执行以下操作:

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

You can check angularfire docs and firebase docs on filtering.

您可以查看angularfire文档和firebase文档的过滤。

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

但是,这种情况将主要受益于数据库的不同结构,如本文所述。如果问题是存储在其中的键,那么通过他们的$id访问问题就会容易得多,在这种情况下,您可以简单地这样做

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