另一个变量改变时,变量没有改变

时间:2017-08-15 04:26:39

标签: angularjs

我正在尝试将sessionlength转换为秒,并将其保存在名为totalTime的变量中(totalTime = sessionlength * 60)。 在我的脚本中,用户可以单击按钮来增加/减少sessionlength。 但问题是totalTime没有随sessionlegnth改变而改变。 任何人都可以指出我哪里出错了?提前谢谢!

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++;
    };  
  });

1 个答案:

答案 0 :(得分:2)

您不会重新计算会话长度更改后的总时间,您可以这样做: -



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>
&#13;
&#13;
&#13;