【发布时间】:2014-04-08 21:29:38
【问题描述】:
我正在尝试使用 AngularJS 在表单中显示三个选择下拉列表(以指示生日 - 月、日和年)。然后,此表单需要将这些值发布到我的 API。
我有一个包含每个月、日和年的数组,我像这样填充控制器:
$scope.months = myMonthArray;
$scope.user.birthMonth = $scope.months[0];
$scope.days = myDayArray;
$scope.user.birthDay = $scope.days[0];
$scope.years = myYearArray;
$scope.user.birthYear = $scope.years[0];
$scope.register = function(user) {
$scope.errors = [];
repository.register(user).$promise.then(
function() { $location.url('/'); },
function(response) { $scope.errors = response.data;});
};
我的存储库工厂方法如下所示:
register: function(user) {
var reg = $resource('/api/registration', null, {
register: { method: 'POST'}
});
return reg.save(user);
}
最后,视图(对于 select 如下所示:
<select id="birthMonth" ng-model="user.birthMonth" ng-options="c.name for c in months"></select>
<select id="birthDay" ng-model="user.birthDay" ng-options="c.name for c in days"></select>
<select id="birthYear" ng-model="user.birthYear" ng-options="c.name for c in years"></select>
视图看起来很棒,因为每个选择框都正确填充并显示了第一个值。但是,当我提交它时,我的 .NET APT 模型无法捕获这些值 - 查看 Fiddler 请求,它看起来如下所示:
{"birthMonth":
{"name":"November","value":11},
"birthYear":
{"name":1988,"value":1988},
"birthDay":
{"name":23,"value":23}}
表单中的所有其他字段都正确捕获(假设它们只是文本输入),所以我认为问题在于 Angular 提供值数组而不是简单地发布每个选择控件的值。
是不是我为每个错误指定了模型?或者我需要做些什么不同的事情才能只传递每个选择字段的值?
谢谢!
【问题讨论】: