我最初有一个值 - 我们称它为 x。选择增量按钮应该将“+1”增加到该值。如果我选择递减,x 应该减少 -1。
但是,实际发生的情况是,当我按下递增按钮时,它会增加 +1,但如果我单击递减,它会减少 -2。它只能增加/减少 1 个值。也不需要连续迭代(count++ 和 count--)。如果将“count”作为 .js 文件中的变量,而不是在 html 中将其作为 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>
关于javascript - 我如何增加和减少angularjs中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27227535/