如何用Firebase替换parseInt,$ stateparams等?

时间:2016-12-07 16:56:46

标签: javascript angularjs firebase firebase-realtime-database

所以我有这种HTML情况

<ion-view title="Question# {{questionNo}}">
  <ion-nav-bar class="bar bar-header bar-dark">
  </ion-nav-bar>

<ion-content class="quest-container">
    <h2>{{question}}</h2>
    <ol>

    </ol>
    <br>
    <a class="button-assertive button" href="#/{{questionNo + 1}}" ng-disabled="nextRoute()">Next</a>
    <a class="button-energized button" ui-sref="score" ng-disabled="!nextRoute()">Finish</a>
</ion-content>

它显示如下:

Screenshot

以及控制器和JSON数据库:

    .controller('questionController',function($scope, $stateParams, $state){
    $scope.questions = [
    {
        nama:"What is your name?",
        answers:['Ahmed','Saad','Farhan'],
        correctAnswer:2
    },
    {
        nama:"How old are you?",
        answers:['22','23','24'],
        correctAnswer:3
    },
    {
        nama:"Where do you live?",
        answers:['Karachi','lahore','Islamabad'],
        correctAnswer:1
    },
    {
        nama:"How many apps you have developed?",
        answers:['<10','10','>10'],
        correctAnswer:2
    },
    {
        nama:"Which car you have?",
        answers:['Chevrolet','Mercedes Benz','Toyota','Tesla'],
        correctAnswer:2
    }

];

    $scope.nextRoute = function(){
        if(parseInt($stateParams.questionId) +1 > $scope.questions.length){
            return true;
            console.log("disable");
        }
        else{
            console.log("enable");
            return false;
        }
    }

    $scope.questionNo = parseInt($stateParams.questionId);
    $scope.question = $scope.questions[parseInt($stateParams.questionId) - 1].nama;
    $scope.answers = $scope.questions[parseInt($stateParams.questionId) - 1].answers;
    $scope.totalQuestions = $scope.questions.length;
    $scope.currentRoute = parseInt($stateParams.questionId);

    function answerChoice(){}

});

然后我做了一些迁移数据库到Firebase,它存储在Questions表中,我试图以这种方式调用数据库:

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

$scope.nextRoute = function(){
    // i dont know how to replace parseInt($stateParams.questionId) with my fireBaseData, and also call $scope.questions.length)
    if(parseInt($stateParams.questionId) +1 > $scope.questions.length){
        return true;
        console.log("disable");
    }
    else{
        console.log("enable");
        return false;
    }
}

// didnt work using this syntax
$scope.questionNo = $scope.questions.$id;
//also these kind parseInt and $stateParams things
$scope.question = $scope.questions[parseInt($stateParams.questionId) - 1].nama;
$scope.answers = $scope.questions[parseInt($stateParams.questionId) - 1].answers;
$scope.totalQuestions = $scope.questions.length;
$scope.currentRoute = parseInt($stateParams.questionId);

//the rest i think i could work on it if i got the answer for questions above
function answerChoice(){}

我的问题是:

  1. 如何调用我的Firebase表ID(之前我使用的是parseInt($stateParams.questionId))?
  2. 如何像以前一样计算我的数据库长度(之前我使用的是parseInt($stateParams.questionId),但现在却算0)?

1 个答案:

答案 0 :(得分:0)

尝试使用此语法

$scope.questions.$loaded().then(function() {
    // didnt work using this syntax
    $scope.questionNo = $scope.questions.$id;
    //also these kind parseInt and $stateParams things
    $scope.question = $scope.questions[parseInt($stateParams.questionId) - 1].nama;
    $scope.answers = $scope.questions[parseInt($stateParams.questionId) - 1].answers;
    $scope.totalQuestions = $scope.questions.length;
    $scope.currentRoute = parseInt($stateParams.questionId);
});