目标:页面刷新后游戏分数增加

时间:2017-02-06 07:55:24

标签: angularjs cookies

我有一个可以正常工作并增加分数的控制器 然而,在页面刷新后,分数会恢复为0,如何使用cookie获得值?

var myApp = angular.module(' ScoreTestApp',[' ngCookies']);

   myApp.controller('ScoreTestCtrl', ['$scope','$cookies', function                ($scope,$cookies) {

      $scope.score = 0;

      $scope.initializeGame = function () {


               $scope.noOfTries = 0;

               $scope.original = $scope.pageTitle;
               $scope.guess = null;
               $scope.deviation = null;

               $scope.verifyGuess = function () {
                   $scope.deviation = $scope.original - $scope.guess;

                   $scope.noOfTries = $scope.noOfTries + 1;
                   $scope.falseAnswer = $scope.deviation != 0;
                   if ($scope.deviation == 0 && $scope.noOfTries == 1){
                      $scope.score += 1;

                 // Setting a cookie

                    $cookies.test =  $scope.score;

                    // Retrieving a cookie
                    var test = $cookies.test;
                    $scope.update = test;

                };

0 个答案:

没有答案