I am facing a problem with the binding of a radio button to the MVC model when using AngularJS, all other fields are binding successfully to the MVC model but the radio button is just not binding. Please advice where I got my wires mixed up.
HTML
<tr ng-repeat="item in experienceModel">
<td><input type="text" name="StartDate" ng-model="item.StartDate | date:'dd-MM-yyyy'" id="date_teachingStart{{$index}}"/></td>
<td><input type="text" name="EndDate" ng-model="item.EndDate | date:'dd-MM-yyyy'" id="date_teachingEnd{{$index}}"/></td>
<td><input type="text" name="SubjectArea" ng-model="item.SubjectArea"/></td>
<td><input type="text" name="Position" ng-model="item.Position"/></td>
<td><input type="text" name="Institution" ng-model="item.Institution"/></td>
<td><input type="text" name="City" ng-model="item.City"/></td>
<td><input type="text" name="Country" ng-model="item.Country"/></td>
<td class="centerAlign"><input type="radio" name="item.IsCurrent" value="{{$index}}" ng-model="selected.item" id="radio_experience[{{$index}}]" /></td>
<td><a class="removeRow" title="Delete item" href="" ng-click="removeRow()"></a></td>
<td><input type="hidden" name="CVId" ng-model="item.CVID" /></td>
<td><input type="hidden" name="ExperienceID" ng-model="item.ExperienceID" /></td>
</tr>
script
var experienceModel = <%: Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model.TeachingExperience)) %>
app.value("experienceModel", experienceModel);
Angular Controller
app.controller('TeachingController', ['$scope', 'experienceModel',
function ($scope, experienceModel) {
$scope.counter = 0;
$scope.selected = {};
$scope.experienceModel = experienceModel;
// Set the selected value to reflect initial data
$scope.experienceModel.forEach(function (item, index) {
if (item.IsCurrent) {
$scope.selected.item = index;
}
});
if ($scope.experienceModel == null) {
$scope.experienceModel = [{ 'ExperienceID': '', 'CVID': '', 'ExperienceCategoryId': '', 'StartDate': '', 'EndDate': '', 'SubjectArea': '', 'NoOfDays': '', 'Position': '', 'Programme': '', 'KnowledgeArea': '', 'Institution': '', 'Client': '', 'City': '', 'Country': '', 'IsCurrent': '' }];
}
if ($scope.experienceModel.length == 0) {
$scope.experienceModel.push({ 'ExperienceID': '', 'CVID': '', 'ExperienceCategoryId': '', 'StartDate': '', 'EndDate': '', 'SubjectArea': '', 'NoOfDays': '', 'Position': '', 'Programme': '', 'KnowledgeArea': '', 'Institution': '', 'Client': '', 'City': '', 'Country': '', 'IsCurrent': '' });
}
$scope.$watch('selected.item', function (index) {
$scope.items.forEach(function (item, i) {
item.isCurrent = i === parseInt(index);
});
});
$scope.counter = 1;
$scope.addRow = function () {
$scope.experienceModel.push({ 'ExperienceID': '', 'CVID': '', 'ExperienceCategoryId': '', 'StartDate': '', 'EndDate': '', 'SubjectArea': '', 'NoOfDays': '', 'Position': '', 'Programme': '', 'KnowledgeArea': '', 'Institution': '', 'Client': '', 'City': '', 'Country': '', 'IsCurrent': '' });
$scope.counter++;
}
$scope.removeRow = function () {
var row = $(this);
$scope.experienceModel.splice(row[0].$index, 1);
$scope.counter--;
}
}]);
ng-modelinstead of the value attribute?