angularjs - variable didn't change while another variable changes -
i'm trying convert sessionlength
seconds , save in variable called totaltime
(totaltime
= sessionlength
* 60). in script, user can click button increase/decrease sessionlength
. problem totaltime didn't change sessionlegnth
changed. can point out did go wrong? in advance!
var myapp = angular.module('myapp', []); myapp.controller('pomodorotimer',function pomodorotimer($scope) { $scope.breaklength = 5; $scope.sessionlength = 25; $scope.totaltime = $scope.sessionlength * 60; $scope.decreasenumber = function() { $scope.sessionlength--; }; $scope.increasenumber = function() { $scope.sessionlength++; }; });
you not recalculating total time after session length changes, can this:-
var app = angular.module("myapp", []); app.controller("myctrl", function($scope) { $scope.breaklength = 5; $scope.sessionlength = 25; $scope.calctotaltime = function(){ $scope.totaltime = $scope.sessionlength * 60; } $scope.decreasenumber = function() { $scope.sessionlength--; $scope.calctotaltime(); }; $scope.increasenumber = function() { $scope.sessionlength++; $scope.calctotaltime(); }; $scope.calctotaltime(); });
<!doctype html> <html> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.4/angular.min.js"></script> <body> <div ng-app="myapp" ng-controller="myctrl"> <button ng-click="increasenumber()">increase</button> <button ng-click="decreasenumber()">decrease</button> <br> session length:{{sessionlength}}<br> total time:{{totaltime}} <br> </div>
Comments
Post a Comment