2017-10-17 86 views
0

现在,我的代码有点问题。乘法&复选框的值

我有一个问卷,单选按钮的答案。它们的值是一个系数。全部记录在Mongodb中。

这里是我的html代码:

<!--réponses--> 
<div ng-repeat="r in q.reponses" class="col-lg-12"> 
    <div class="radio" data-placement="bottom-left"> 
     <label class="radio" id="labelRep" for="{{r.coefficient}}"> 
      <input type="radio" ng-change="updateCounter(r.coefficient)" ng-model="reponseQuestionnaire.userAnswers[r.code]" value="{{r.coefficient}}" id="{{r.coefficient}}" class="custom-checkbox"/> 
       {{r.reponse}} 
     </label> 
    </div> 
</div> 

我想做的事情:

我想乘以对应勾选答案系数。

你知道一个函数吗?

我在我的控制器,但没有成功尝试这个功能:

$scope.counter = 0; 

$scope.updateCounter = function ($event, coefficient, value){ 
     coefficient = parseInt(coefficient, 10); 
     if (value === 0) { 
      $scope.counter += coefficient; 
     } 
     else{ 
      $scope.counter -= coefficient; 
     } 

预先感谢您。

此致敬礼。

请注意,我使用的技术是AngularJS。

回答

0

ng-change的第一个参数是任何你传递给它的调用函数而不是$event

改变这种

$scope.updateCounter = function ($event, coefficient, value)

$scope.updateCounter = function (coefficient)

和功能将被正确调用。

+0

好吧,谢谢你..!和功能?你有好主意吗 ? – Nadjeem

+0

@Nadjeem有什么功能? – Exlord

+0

进行处理,有什么功能?它包含什么?如果我删除参数中的“值”,我有“价值没有定义”... – Nadjeem

0

修改代码,以下

的Html

ng-change="updateCounter(r.coefficient,value)"` 

控制器

$scope.updateCounter = function (coefficient, value){ 
    coefficient = parseInt(coefficient, 10); 
    if (value === 0) { 
     $scope.counter += coefficient; 
    } 
    else{ 
     $scope.counter -= coefficient; 
    } 
+0

谢谢!但是这让我想起了NaN ..你有什么想法可能来自哪里? – Nadjeem

+0

这可能是因为从html发送的值未定义,因此在控制器中将其设置为零 – Vignesh

+0

由于您没有在html或控制器中的任何位置指定该值,因此该值来自哪里 – Vignesh