我看到了很多回复,但都使用了$scope
我有几个ng-models
有几个按钮来增加或减少值,我想只使用一个函数。
像这样:
app = angular.module("myApp",[]);
app.controller("myCtrl", mainFunction);
function mainFunction(){
scope = this;
this.value1 = 3;
this.value2 = 5;
this.addOne = function (control) {
if (scope.control <12){
scope.control += 1;
}
}
this.dimOne = function (control) {
if (scope.control > 1) {
scope.control -= 1;
}
}
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl as vm">
<span>{{vm.value1}}</span>
<button ng-click="vm.dimOne('value1')">-</button>
<button ng-click="vm.addOne('value1')">+</button>
<br />
<span>{{vm.value2}}</span>
<button ng-click="vm.dimOne('value2')">-</button>
<button ng-click="vm.addOne('value2')">+</button>
</div>
&#13;
答案 0 :(得分:1)
只需传递实际的model
: -
<button ng-click="vm.dimOne(vm.value2)">-</button>
*.js
this.dimOne = function (control) {
if (control > 1) {
control -= 1;
this.value2 = control ;
}
}
答案 1 :(得分:0)
由于您似乎传递了所需的字符串,只需更新您的功能:
package com.github.biniama
class TestController {
TestService testService
def index() {
Integer result = testService.getData()
render "Returned value is ${result}"
}
}
另外......你的html应该是这个吗?
this.addOne = function (control) {
if (scope[control] < 12) {
scope[control] += 1;
}
}
this.dimOne = function (control) {
if (scope[control] > 1) {
scope[control] -= 1;
}
}