Select 元素值未与 Angular 中的其他表单元素一起发送
Select element value not being sent with other form elements in Angular
我正在使用 Angular 将表单内容提交到 API 端点。一切正常,直到我尝试将其中一个文本输入字段更改为 select 下拉列表。我正在使用 ng-options
来填充 select 下拉列表,但是当我将表单内容发送到 API 端点时, select 元素的值未发送。下面是 HTML(为简洁起见删除了 class 和样式信息)和 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>
Angular
<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
定义为 select
元素的 ng-model
,为什么它没有作为表单集合的一部分提交?
尝试改变这个
ng-options="dept.Name as dept.Name for dept in self.departmentList">
我正在使用 Angular 将表单内容提交到 API 端点。一切正常,直到我尝试将其中一个文本输入字段更改为 select 下拉列表。我正在使用 ng-options
来填充 select 下拉列表,但是当我将表单内容发送到 API 端点时, select 元素的值未发送。下面是 HTML(为简洁起见删除了 class 和样式信息)和 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>
Angular
<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
定义为 select
元素的 ng-model
,为什么它没有作为表单集合的一部分提交?
尝试改变这个
ng-options="dept.Name as dept.Name for dept in self.departmentList">