如何增加和减少angularjs中的值? [英] How can I increment and decrement a value in angularjs?
问题描述
我最初有一个值 - 让我们称之为x。选择增量按钮应该增加+1值。如果我选择减量,则x应减少-1。
I have one value initially - let's call it x. Selecting the increment button should increase "+1" to the value. If I select decrement, x should decrease by -1.
然而,实际发生的是,当我按下增量按钮时,它会增加+1,但如果我点击减少,它会减少-2。它应该只增加/减少1个值。也不需要连续迭代(count ++和count--)。如果将count视为.js文件中的变量,在html中没有提到它作为ng-init =count = 15会更好。
However, what actually happens is that when I press the increment button, it increases +1 but if i click decrement, it decreases -2. It should only be increased/decreased by 1 value only. Also don't require continuous iteration (count++ and count--). it would be better if "count" is taken as variable inside .js file, not mentioning it in html as ng-init="count=15" .
<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>
推荐答案
这应该可行,
<div>
<button ng-click="count = count+1" ng-init="count=15">
Increment
</button>
count: {{count}}
<button ng-click="count = count - 1">
Decrement
</button>
<div>
这篇关于如何增加和减少angularjs中的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!