0

View is not updating when my scope in controller is changing. I am actually trying to use a timer in my html. When timer is counting down, it is not updating in the view.

here is my view:

<div class="quiz-container" ng-controller="KbcQuizController">
<div class="clockdiv">
        <div class="timer">
            <span class="second">{{timer.value}}</span> <img class="timer-icon"
                src="/app/images/timer.png">
        </div>
    </div>
</div>

Below is my controller:

(function() {

    'use strict';

    angular.module('app.kbcquiz').controller('KbcQuizController',
            KbcQuizController);

    KbcQuizController.$inject = [ '$timeout', '$rootScope', '$scope', '$http',
            '$filter', 'ngDialog', 'usSpinnerService', 'quizService', '$state' ];
    function KbcQuizController($timeout, $rootScope, $scope, $http, $filter,
            ngDialog, usSpinnerService, quizService, $state) {
$scope.timer = {
        value : 60
    }
    $scope.counter = 60;
    var timeinterval;
    var mytimeout;
    $scope.onTimeout = function() {
                $scope.timer.value--;
                console.log("counter is::" + $scope.timer.value);
                mytimeout = $timeout($scope.onTimeout, 1000);
                if ($scope.timer.value == 0) {
                    alert("timeout");
                    $scope.stop();
                }
            }

            $scope.start = function() {
                $scope.timer.value = 60;
                mytimeout = $timeout($scope.onTimeout, 1000);
            }

            $scope.stop = function() {
                alert("in stop")
                $timeout.cancel(mytimeout);
            }

            $scope.startQuiz = function() {
                quizService.getQuestion($scope.count, timer.reset, timer.start)
                        .then(null, function(err) {
                            console.log("error in get question");
                        });
                $scope.start();
            }
}
})();

I am calling the startQuiz() method in controller from a dialog box. Below is the snippet:

<h3 class="dialog_header">Welcome to KBC!!</h3>
<div class="dialog-contents">
    <div class="ngdialog-message">
        <div>
            <div class="next-button">
                <button type="submit"
                    class="ngdialog-button ngdialog-button-primary"
                    ng-click="startQuiz(); closeThisDialog('button')">Start Quiz</button>
            </div>
        </div>
    </div>
</div>

Below is my module:

(function() {
    'use strict';
    var module = angular.module('app.kbcquiz', [ 'ui.router',
            'angularUtils.directives.dirPagination', 'ng-bootstrap-datepicker',
            'ngDialog', 'angularSpinner' ]);


    module.config(appConfig);

    appConfig.$inject = [ '$stateProvider' ];

    function appConfig($stateProvider) {
        $stateProvider.state('app.kbcquiz', {
            url : '/rules',
            templateUrl : 'app/modules/kbcquiz/instructions.html',
        //  controller : 'KbcQuizController',
        //  controllerAs : 'vm'
        })

        .state('quizpage', {
            url : '/app/kbc-quiz',
            templateUrl : 'app/modules/kbcquiz/quiz-list.html',
        //  controller : 'KbcQuizController',
        //  controllerAs : 'vm'

        });

    }
})();

Please let me know where I am going wrong.

1 Answer 1

0

Have you add the ng-app directive in your index.html like this

<html ng-app="app.kbcquiz">
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, i have given.
can you publish all your code on a codepen for example because without more information it's hard to give you a response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.