Cordova.writefile函数不运行,但不会出错

时间:2017-01-12 03:46:42

标签: angularjs cordova ionic-framework cordova-plugins visual-studio-cordova

我正在进行离子应用。我有一个名为scope.win()的函数,它应该在测验完成后触发,然后更新JSON文件以将该测验添加到完成的测验数量但它无法正常运行。它不会放弃任何错误。该功能在某个时刻停止运行,应用程序继续运行,没有任何事情发生 这是控制器的文件

    angular.module('controllers', ['services'])

 .controller('MainCtrl', function ($scope, $state, data) {
$scope.$on('$ionicView.enter', function () {
    data.create();

});
$scope.chapter = data.chapterProgress();



 })
 .controller("BtnClick", function ($scope, lives, data, $cordovaFile, $ionicScrollDelegate) {
var live = 3;
var clickedOn = [];
var numQuestions;
$scope.part2Cred = false;
$scope.part3Cred = false;
$scope.part4Cred = false;
$scope.part5Cred = false;
$scope.part6Cred = false;
$scope.part7Cred = false;
$scope.part8Cred = false;
$scope.part9Cred = false;
$scope.part10Cred = false;
$scope.partQCred = false;

$scope.setNumQuestions = function(num){
    numQuestions = num;
}

$scope.updatelives = function (){
    //grabs the element that is called liv then updates it
    var livesOnPage = document.getElementById('liv');
    livesOnPage.innerHTML = live;
}
$scope.wrong = function (event){
    var selec = document.getElementById(event.target.id);
    if(clickedOn.includes(selec)){}
    else{
        selec.style.color = "grey";
        clickedOn.push(selec);
        live = live - 1;
        if(live == 0){
            $scope.gameover();
        }
        else{
            $scope.updatelives();
        }
    }
}
$scope.right = function (event,chapter, section){
    var selec = document.getElementById(event.target.id);
    if(clickedOn.includes(selec)){}
    else{
        selec.style.color = "green";
        clickedOn.push(selec);
        numQuestions = numQuestions - 1;
        if(numQuestions === 0){


            $scope.win(chapter, section);


        }
    }
}
$scope.gameover = function(){
    alert("game over please try again");
    live = 3;
    $ionicScrollDelegate.scrollTop();
    $scope.partQCred = false;
    $scope.part1Cred = !$scope.part1Cred;
    for(i = 0; i< clickedOn.length;i++){
        clickedOn[i].style.color = "rgb(68,68,68)";
    }

}
$scope.win = function (chapter, section) {
    alert("Well Done");
    var data = data.chapterProgress(); // It is at this point that the //function stops running without giving off any error.
    alert("Good Job");
    var sectionsComplete = data[chapter].sectionsCompleted;
    var totalsection = data[chapter].totalSections;

    if (section === totalSection) {
        window.location.href = "#/chapter1sections";
        return;
    }
    if (section > sectionsComplete) {
        data[chapter].sectionsCompleted += 1;
        var url = "";
        if (ionic.Platform.isAndroid()) {
            url = "/android_asset/www/";
        }

        document.addEventListener('deviceready', function () {
            $cordovaFile.writeFile(url + "js/", "chapters.json", data, true)
      .then(function (success) {
          // success
          alert("Json file updated")
          window.location.href = "#/chapter1sections";
      }, function (error) {
          // error

      });
        });
    }
}



});

这是服务文件。它看起来运行正常,但在控制器中有问题的代码部分引用了很多,所以我认为有必要把它放在这里。

    angular.module('services', [])
.service('lives', function () {
    var live = 3;

    return {
        getlives: function () {
            return live;
        },
        setlives: function (value) {
            live = value;
        }
    };
})
.service('data', function ($cordovaFile, $http) {
    var url = "";
    if (ionic.Platform.isAndroid()) {
        url = "/android_asset/www/";
    }
    return {
        //Run this function on startup to check if the chapters.json file exists, if not, then it will be created
        create: function () {
            var init = {
                "one": {
                    "sectionsCompleted": 0,
                    "totalSections": 4
                },
                "two": {
                    "sectionsCompleted": 0,
                    "totalSections": 1
                }
            };

            $cordovaFile.writeFile(url + "js/", "chapters.json", init, false)
  .then(function (success) {
      // success

  }, function (error) {
      // error

  });

        },

        chapterProgress: function () {
           return $http.get(url + "js/chapters.json").success(function (response) {
                var json = JSON.parse(response);
                return json;
            }, function (error) {
                alert(error);
            });
    }

    }
});      

非常感谢您的帮助和时间。

0 个答案:

没有答案