0

I have a scenario where i need to show a div when the timer total value is < 0 Here is the directive code:

angular.module('iSourcingApp.tpModule')
.directive('stopWatch', function($state) {
    return {
        restrict: 'A',
        replace: false,
        scope: {
            name: "=",
            timeOfInterview: "=",
            onSend: '&',
            startInterview: '&',
            viewPage: "="
        },
        controller: function($scope, $rootScope, $interval) {
            debugger
            $rootScope.showDiv = {};
            $scope.getTimeRemaining = function(endtime) {
                $scope.t[$scope.name].total = Date.parse(endtime) - Date.parse(new Date());
                $scope.t[$scope.name].seconds = Math.floor(($scope.t[$scope.name].total / 1000) % 60);
                $scope.t[$scope.name].minutes = Math.floor(($scope.t[$scope.name].total / 1000 / 60) % 60);
                $scope.t[$scope.name].hours = Math.floor(($scope.t[$scope.name].total / (1000 * 60 * 60)) % 24);
                $scope.t[$scope.name].days = Math.floor($scope.t[$scope.name].total / (1000 * 60 * 60 * 24));
            }
            $scope.initializeClock = function(endtime) {
                debugger
                $scope.t = {};
                $scope.t[$scope.name] = {};
                $scope.updateClock = function() {
                    debugger
                    $scope.getTimeRemaining(endtime);
                    $scope.t[$scope.name].hours = ('0' + $scope.t[$scope.name].hours).slice(-2);
                    $scope.t[$scope.name].minutes = ('0' + $scope.t[$scope.name].minutes).slice(-2);
                    $scope.t[$scope.name].seconds = ('0' + $scope.t[$scope.name].seconds).slice(-2);
                    if ($scope.t[$scope.name].total == 0) {
                        console.log($scope.t[$scope.name].total);
                        $interval.cancel($scope.timeinterval);
                        $rootScope.showDiv[$scope.name] = true;
                        alert("Start interview for " + $scope.name);
                    } else {
                        if ($scope.t[$scope.name].total < 0) {
                            $interval.cancel($scope.timeinterval);
                            $rootScope.showDiv[$scope.name] = true;
                        }
                    }
                }
                $scope.updateClock();
                $scope.timeinterval = $interval($scope.updateClock, 1000);
            }
            $scope.initializeClock($scope.timeOfInterview);
        },
        templateUrl: function() {
            var tpl = $state.current.name;
            return './tpModule/views/' + tpl + '.html';
        }
    };
});

Here if $scope.t[$scope.name].total < 0then i am setting showDiv to true and when it is true i m showing a div

<div ng-show="showDiv[candidateInfo.name]" class="col-xs-offset-4 showDiv">
            <p class="timer-text">The interview for {{candidateInfo.name}} has crossed scheduled time</p>
            <div class="row showDiv" >
                <a class="timer-text col-xs-3" style="cursor:pointer" ng-click="fnReschedule(candidateInfo.name, candidateInfo.recruiter)"><i >reschedule</i></a>
                <a class="timer-text col-xs-3" style="cursor:pointer" ng-click="fnStartInterview(candidateInfo.name,candidateInfo.presentRound,candidateInfo.askedQuestions,candidateInfo._id,candidateInfo.dateOfInterview)"><i class="text-center">start interview</i></a>
            </div>
        </div>

So,what is happening here is initially some template is shown and after a sec or so the div is shown but i need not show the template initially if the timer is < 0

and the timer is different for different candidates

Any help would be appreciated.

Shikha thakur
  • 1,269
  • 13
  • 34
  • try wrapping $rootScope.showDiv[$scope.name] = true; in $timeout block – Frane Poljak Aug 05 '16 at 10:50
  • And "showDiv" should be an attribute, not a class name, since your directive's "restrict" is set to "A" – Frane Poljak Aug 05 '16 at 10:52
  • @FranePoljak actually showDiv is a class in which i gave padding and margin to the content and could u plz elaborate wrapping $rootScope.showDiv[$scope.name]=true in $timeout – Shikha thakur Aug 05 '16 at 11:02
  • 1
    Sorry, I thought your directive was named showDiv, but it's named stopWatch, and I don't see it applied anywhere. I guess you should do something like:
    . The $timeout takes care of digest cycles, and I prefer it over scope.$apply() (in your case $rootScope.$apply()) because it avoids a "digest already in progress" error. http://stackoverflow.com/a/23073968/1630623
    – Frane Poljak Aug 08 '16 at 11:39
  • yes i did in the same way and i got the result Thanks:) – Shikha thakur Aug 08 '16 at 11:43

0 Answers0