1

I have a dynamic table that creates the amount of rows based on the user selection. Each row in the table then has input boxes for numbers. If I have a table with 7 rows for example, I want to store those 7 different inputs into an array. So far I am trying to pass the input of the textbox to a function which updates declared blank arrays. So something like this

HTML

 <td id="{{'redScore'+($index+1)}}">
 <input required="" ng-change="updateRedScore(inputValue)" ng-model="inputValue" type="number" step="1" name="rate" min="1" max="10"> </td>

Script

$scope.redRoundScore = [];    
$scope.inputValue = null;

$scope.updateRedScore = function(passedscore){
   $scope.redRoundScore[index] = passedscore
 }

Is there a way I can pass the index alongside the inputValue to updateRedScore?

3
  • 1
    Could you not just pass $index as a second parameter on your updateRedScore() method? This is all inside an ng-repeat, right? Commented Feb 7, 2018 at 22:56
  • Yes! Sorry I am brand new to angular / js and was just trying to pass index before instead of $index. I will try that and report back Commented Feb 7, 2018 at 22:57
  • @Lex Yes indeed that works, so simple :/ Thanks Commented Feb 7, 2018 at 22:59

1 Answer 1

1

In the interest of completing this question/answer that may help others in the future, adding $index as a parameter to the method should work. Also, the $scope.inputValue = null; is not needed since the inputValue variable only exists on the scope that is created for the ng-repeat.

HTML:

<td id="{{'redScore'+($index+1)}}">
    <input required 
           ng-change="updateRedScore(inputValue, $index)" 
           ng-model="inputValue" 
           type="number" 
           step="1" 
           name="rate" 
           min="1" 
           max="10">
</td>

JS:

$scope.redRoundScore = [];
$scope.updateRedScore = function(passedscore, index) {
    $scope.redRoundScore[index] = passedscore
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.