I'm having the same problem as in this question, but the solutions there do not work for me.
I'm learning AngularJS and I am struggling to submit form data using ng-model
inside of ng-repeat
. I have a list of members of a group and the date they last attended:
[{"name": "Bob", "attended": "01/01/16"},
{"name":"Steve", "attended": "02/01/16"}]
I'm listing out all members using ng-repeat
, and would like to be able to update the "attended" field from a form in each line. However, I cannot get "attended" to bind properly. Here's the code:
<div ng-repeat="member in members">
{{member.name}}
<input type="text" ng-model="attended"></input>
<a href="#" ng-click="setAttended(attended)">Save</a>
</div>
$scope.setAttended = function(attended){
alert( 'Date: ' + attended);
};
The alert shows "Date: undefined". I've also tried using objects instead of primitives:
<div ng-repeat="member in members">
{{member.name}}
<input type="text" ng-model="member.attended"></input>
<a href="#" ng-click="setAttended(member)">Save</a>
</div>
$scope.setAttended = function(member){
alert( 'Date: ' + member.attended);
};
I get that ng-repeat
will create a new scope for each object, and those scopes inherit setAttended
from the parent scope. I get that the parent scope doesn't know what is happening in the child scopes, and so the parameter must let it know.
Why doesn't my parameter contain my input value?
Is the input not being bound to the model correctly before I run setAttended
?