I have created a stopwatch factory service, doesn't really do much besides running (reset and other features not implemented, just ignore them).
I set a $scope.time
to receive the changes of the timer, however it isn't being updated.
I have tried many different approaches the last one I guess is really bad practice but I wanted to try and make it work.
my view:
<ion-view view-title="Clock">
<ion-content>
<div ng-controller="controller as ClockCtrl">
<h1 ng-bind="ClockCtrl.time"></h1>
</div>
</ion-content>
</ion-view>
my Controller:
.controller('ClockCtrl', function($scope, Stopwatch) {
var vm = $scope;
$scope.time = "";
Stopwatch.init($scope);
})
my service:
factory('Stopwatch', function() {
var Stopwatch = (function() {
var s;
var isRunning = 0;
var time = 0;
var thatElement;
function run(element){
if(isRunning !== 0){
time++;
s.mills = time % 10;
s.secs = Math.floor(time / 10);
s.mins = Math.floor(time / 10 / 60);
//Stopwatch.currentTime = ("0"+s.mins).slice(-2)+":"+("0"+s.secs).slice(-2)+":"+("0"+s.mills).slice(-2);
thatElement.time = ("0"+s.mins).slice(-2)+":"+("0"+s.secs).slice(-2)+":"+("0"+s.mills).slice(-2);
setTimeout(run, 100);
}
};
return {
settings: {
currTime: "",
mills: 0,
secs: 0,
mins: 0,
i: 1,
times: ["00:00:00"],
},
currentTime: "",
init: function(element) {
thatElement = element;
s = this.settings;
this.start(element);
},
clear: function() {
s.i = 1,
s.mins = s.secs = s.mills = 0;
s.times = ["00:00:00"],
s.results.innerHTML = s.clearButton;
},
start:function(element){
isRunning = 1;
run(element);
},
stop: function(){
isRunning = 0;
}
}
})();
return Stopwatch;
})
I know that the last approach is bad, I would accept any pointers, this is my first time using ionic, 2nd time trying angular.
Thanks in advance!