简体   繁体   English

如何增加和减少angularjs中的值?

[英]How can I increment and decrement a value in angularjs?

I have one value initially - let's call it x. 我最初有一个值 - 让我们称之为x。 Selecting the increment button should increase "+1" to the value. 选择增量按钮应该增加“+1”值。 If I select decrement, x should decrease by -1. 如果我选择减量,则x应减少-1。

However, what actually happens is that when I press the increment button, it increases +1 but if i click decrement, it decreases -2. 然而,实际发生的是,当我按下增量按钮时,它会增加+1,但如果我点击减少,它会减少-2。 It should only be increased/decreased by 1 value only. 它应该只增加/减少1个值。 Also don't require continuous iteration (count++ and count--). 也不需要连续迭代(count ++和count--)。 it would be better if "count" is taken as variable inside .js file, not mentioning it in html as ng-init="count=15" . 如果将“count”作为变量放在.js文件中,而不是在html中将其称为ng-init =“count = 15”,那会更好。

JsBin JsBin

<div ng-controller="CounterController">
  <button ng-click="count = {{count}}+1" ng-init="count=15">
    Increment
  </button>
  count: {{count}}

  <button ng-click="count = {{(count) - 1}}">
    Decrement
  </button>
<div>

Simply this should work, 这应该工作,

<div>
    <button ng-click="count = count+1" ng-init="count=15">
        Increment
    </button>
    count: {{count}}

    <button ng-click="count = count - 1">
        Decrement
    </button>
<div>

The problem is because you using '{{' in ng-click, it's inserting value there, so after angular 'rendering', actual code looks like: 问题是因为你在ng-click中使用'{{',它在那里插入值,所以在角'渲染'之后,实际代码如下所示:

<div ng-controller="CounterController">
  <button ng-click="count = 15+1" ng-init="count=15">
    Increment
  </button>
  count: {{count}}

  <button ng-click="count = 15 - 1">
    Decrement
  </button>
<div>

but you want to work with reference. 但你想参考。 So just remove '{{' and '}}' and it will work. 所以只需删除“{{”和“}}”即可。

"Exactly 1 value to be incremented or decremented" “正好增加或减少1个值”

<div ng-controller="CounterController">
  <button ng-click="increment();">
    Increment
  </button>
  count: {{count}}

  <button ng-click="decrement();">
    Decrement
  </button>
<div>

Controller: 控制器:

angular.module('myApp', [])
.controller('CounterController', function($scope) {
  var incremented = false;
  var decremented = false;
  $scope.count = 15;

  $scope.increment = function() {
    if (incremented) { return; }
    $scope.count++;
    incremented = true;
  };
  $scope.decrement = function() {
    if (decremented) { return; }
    $scope.count--;
    decremented = true;
  };
});

If you want the user to be able to repeatedly do this, then... 如果您希望用户能够重复执行此操作,那么......

angular.module('myApp', [])
.controller('CounterController', function($scope) {
   $scope.count = 15;
   var max = $scope.count + 1;
   var min = $scope.count - 1;

  $scope.increment = function() {
    if ($scope.count >= max) { return; }
    $scope.count++;
  };
  $scope.decrement = function() {
    if ($scope.count <= min) { return; }
    $scope.count--;
  };
});

JS Fiddle - http://jsfiddle.net/HB7LU/8673/ JS小提琴 - http://jsfiddle.net/HB7LU/8673/

Here is a sample working code and caters to the following: 1) increases and decreases the counter by one 2) does not use continuous iteration 3) does not use ng-init 4) works for $scope.count = 15 下面是一个示例工作代码并满足以下要求:1)将计数器增加或减少一个2)不使用连续迭代3)不使用ng-init 4)适用于$ scope.count = 15

(The replies already provided by Rasalom and smallatom helped and so did the link: http://www.w3schools.com/angular/angular_events.asp ) (Rasalom和smallatom已经提供的回复有助于链接: http//www.w3schools.com/angular/angular_events.asp

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="myCtrl">
    <button ng-click="count = count+1">
        Increment
    </button>
    count: {{ count }}

    <button ng-click="count = count-1">
        Decrement
    </button>

</div>
<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.count = 15;
});
</script> 

</body>
</html>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用jquery设置一个限制来增加和减少值 - how can i set a limit to increment and decrement of value using jquery 如何在pointerdown和pointerup上增加和减少进度条值? - How do I increment and decrement a progress bar value on pointerdown & pointerup? 如何通过快捷方式增加和减少字体大小? (TinyMCE的) - How can I increment and decrement the fontsize via shortcut? (tinymce) 如何在 React 中递增/递减单独的计数器? - How can I increment/decrement separate counters in React? HTML - 如何在HTML页面上创建增量/减量文本框? - HTML - How can I create an increment/decrement textbox on an HTML page? 如何在for循环中使用变量递增或递减? - How can i use a variable to increment or decrement in a for loop? AngularJS-限制增量和减量 - AngularJS - Limit increment and decrement 如何递减和递增值,并显示值 - How to decrement and increment the value, and display value 如何通过事件增加/减少对象的属性值? - How to increment/decrement an objects property value with an event? 如何设置最小 Increment ++ 和 Decrement -- 值(不能低于 1)+ 如何减少或增加按钮附加值 - How to set minimum Increment ++ and Decrement -- value (can't go bellow 1) + how to decrease or increase button added value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM