计算输入字段中的值

时间:2014-11-20 20:27:01

标签: angularjs

我正在尝试使用两个按钮创建一个输入字段,这将在输入字段中添加和分散数字1。

一切正常,但当我尝试直接在输入字段中更改数字时,按钮停止工作

function Ctrl($scope) {
  $scope.Score1 = 0;

  $scope.$watch('Score1', function (newVal) {
    $scope.Score1 = newVal;
    console.log(newVal);
  });

  $scope.add_btn = function() {
    $scope.Score1 ++;
  };

  $scope.dist_btn = function() {
    if ($scope.Score1 > 0) {
      $scope.Score1 --;
    } else {
      $scope.Score1 = 0;
    }
  };
}
</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> 
<link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css">
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
<style>
<div ng-app>
<div ng-controller="Ctrl">
          <button ng-click="add_btn()">+</button>
              <input type="text" value="{{Score1}}">  
          <button ng-click="dist_btn()">-</button>
</div>
</div>

的jsfiddle: http://jsfiddle.net/g9opLkce/

1 个答案:

答案 0 :(得分:2)

使用ng-model绑定输入(demo):

<input type="text" ng-model="Score1">