我正在使用Angular将表单的内容提交到API端点。一切正常,直到我尝试将其中一个文本输入字段更改为选择下拉列表。我使用ng-options
填充选择下拉列表,但是当我将表单内容发送到API端点时,不会发送select元素的值。下面是HTML(为了简洁而删除了类和样式信息)和Angular。
HTML
<form ng-submit="self.addNewEmployee()" novalidate>
<input ng-model="self.form.employeeName" type="text">
<select ng-model="self.form.department" ng-options="dept.name as dept.Name for dept in self.departmentList">
</select>
<input ng-model="self.form.salary" type="text" />
<input id="btnSubmit" type="submit" value="Add Employee" />
</form>
角
<script type="text/javascript">
var adminToolApp = angular.module('adminToolApp', []);
adminToolApp .controller('AdminToolController', function ($scope, $http) {
var self = this;
self.departmentList = [];
// Gets all departments to populate select input
$http({
method: 'GET',
url: 'https://api.myServer.net/api/getAllDepartments',
headers : {
'Content-Type': 'application/json',
}
}).then(function(result) {
self.departmentList = result.data;
}, function (error) {
console.log(error);
});
// Submits form data to add new employee
self.addNewEmployee = function() {
return $http({
method: 'POST',
url: 'https://api.myServer.net/api/addNewEmployee',
data: angular.toJson(self.form),
headers: {
'Content-Type': 'application/json',
}
}).then(_submissionSuccess, _submissionFailure);
};
// Private methods
function _submissionSuccess(response) {
self.submissionResults = response.data;
};
function _submissionFailure(response) {
self.submissionResults = 'An error occured: ' + response.status;
};
});
</script>
当我检查发送到API的有效负载时,会出现两个表单元素(employeeName
和salary
),但部门不存在。我将self.form.department
定义为ng-model
元素的select
,为什么它不作为表单集合的一部分提交?
答案 0 :(得分:1)
尝试更改此
ng-options="dept.Name as dept.Name for dept in self.departmentList">