1

Trying to toggle a button with start and stop functions.

heres the controller

App.controller('LogPhoneMainController',
function LogPhoneMainController($scope) {

var self = this;

$scope.header = "Log a phone call";

$scope.stopwatches = [{ log: [] }, { interval: 1000, log: [] }, { interval: 2000, log: [] }];
});
App.filter('stopwatchTime', function () {
return function (input) {
    if(input){

        var elapsed = input.getTime();
        var hours = parseInt(elapsed / 3600000,10);
        elapsed %= 3600000;
        var mins = parseInt(elapsed / 60000,10);
        elapsed %= 60000;
        var secs = parseInt(elapsed / 1000,10);
        var ms = elapsed % 1000;

        return hours + ':' + mins + ':' + secs + ':' + ms;
    }
};
})
.directive('bbStopwatch', ['StopwatchFactory', function(StopwatchFactory){
return {
    restrict: 'EA',
    scope: true,
    link: function(scope, elem, attrs){   

        var stopwatchService = new StopwatchFactory(scope[attrs.options]);

        scope.startTimer = stopwatchService.startTimer; 
        scope.stopTimer = stopwatchService.stopTimer;
        scope.resetTimer = stopwatchService.resetTimer;

    }
};
}])
.factory('StopwatchFactory', ['$interval',    function($interval){

return function(options){

    var startTime = 0,
        currentTime = null,
        offset = 0,
        interval = null,
        self = this;

    if(!options.interval){
        options.interval = 100;
    }

    options.elapsedTime = new Date(0);

    self.running = false;

    function pushToLog(lap){
        if(options.log !== undefined){
            options.log.push(lap); 
        }
    }

    self.updateTime = function(){
        currentTime = new Date().getTime();
        var timeElapsed = offset + (currentTime - startTime);
        options.elapsedTime.setTime(timeElapsed);
    };

    self.startTimer = function(){
        if(self.running === false){
            startTime = new Date().getTime();
            interval = $interval(self.updateTime,options.interval);
            self.running = true;
        }
    };

    self.stopTimer = function(){
        if( self.running === false) {
            return;
        }
        self.updateTime();
        offset = offset + currentTime - startTime;
        pushToLog(currentTime - startTime);
        $interval.cancel(interval);  
        self.running = false;
    };

    self.resetTimer = function(){
        startTime = new Date().getTime();
        options.elapsedTime.setTime(0);
        timeElapsed = offset = 0;
    };

    self.cancelTimer = function(){
        $interval.cancel(interval);
    };

    return self;

};


}]);

heres my html.

<div class="container">
<div class="row">
    <div ng-controller="LogPhoneMainController" ng-init="init()">
        <h2>{{header}}</h2>
        <div ng-repeat="options in stopwatches|limitTo:1">
            <div bb-stopwatch options="options">
                <div class="container">
                    <div class="stopWatch numbers">
                        {{options.elapsedTime | stopwatchTime}}
                    </div>

                    <button class="btn" ng-click="startTimer()">start</button>
                    <button class="btn" ng-click="stopTimer()">stop</button>
                </div>
            </div>
        </div>
        <div ng-view>

        </div>
    </div>
</div>
</div>

I want the only one btn, and ngclick needs to toggle which function it calls and display iether start or stop accordingly.

UPDATE:

i tried

$scope.startStop = "";

lives in controller and below is in my factory

 self.startStopTimer = function (startStop) {
        if (startStop === "Start") {
            startTimer();
            $scope.startStop = "Stop";
        } else {
            stopTimer();
            $scope.startStop = "Start";
        }
    };

i put that code in my controller, and the html button looks like this

<button class="btn" ng-click="startStopTimer()">
                        {{startStop}}
</button>

but i get scope not defined(edit: i get startStop undefined due to where the function lives)

3
  • instead of scope.startStop = false; use $scope.startStop = false; Commented Aug 6, 2015 at 10:47
  • you need to inject the $scope service into your controller app.controller("MyController", ['$scope', function ($scope) { }]); Commented Aug 6, 2015 at 11:04
  • Scope doesnt exist in my factory, i need to pass $scope.startStop varibale into my factory Commented Aug 6, 2015 at 11:11

3 Answers 3

0

implement the toggle logic in the controller and set the button text withing the controller.

<button class="btn" ng-click="startStopTimer()">{{startStop}}</button>

in the controller implement only one function startStopTimer and based on the value of startStop toggle the text in the button.

Sign up to request clarification or add additional context in comments.

Comments

0

If everything is working fine and you just want to show one button, try this:

<button class="btn" ng-click="running?stopTimer():startTimer()">{{running?'stop':'start'}}</button>

1 Comment

it starts but doesnt stop
0
<button class="btn" ng-click="startTimer(toggleBtn)">{{btn.value}}</button>

and inside the function call toggle the toggleBtn ($scope.toggleBtn = !$scope.toggleBtn) so if parameter is true startTimer() or if false stopTimer() will get executed

or simpler one

You can either use ng-show/ng-hide or ng-if toggle the button with a variable like

<button class="btn" ng-click="toggleBtn = !toggleBtn ; startTimer()" ng-if="toggleBtn">start</button>
<button class="btn" ng-click="toggleBtn = !toggleBtn ; stopTimer()" ng-if="!toggleBtn">stop</button>

$scope.toggleBtm should be true when u want the start button.

1 Comment

sorry could you explain that a little more in the controller side of things?

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.