2017-10-17 35 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