【发布时间】:2015-06-17 19:10:39
【问题描述】:
在使用 AngularJS 时,我遇到了将单选按钮绑定到 MVC 模型的问题,所有其他字段都成功绑定到 MVC 模型,但单选按钮没有绑定。请告知我在哪里弄乱了电线。
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>
脚本
var experienceModel = <%: Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(this.Model.TeachingExperience)) %>
app.value("experienceModel", experienceModel);
角度控制器
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-model而不是value属性吗?
标签: javascript angularjs model-view-controller