0

我正在使用 Angular 将表单的内容提交到 API 端点。在我尝试将其中一个文本输入字段更改为选择下拉列表之前,一切正常。我ng-options用来填充选择下拉菜单,但是当我将表单内容发送到 API 端点时,不会发送选择元素的值。下面是 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 的有效负载时,存在两个表单元素(employeeNamesalary),但是没有部门。我定义self.form.departmentng-modelforselect元素,为什么它不作为表单集合的一部分提交?

4

1 回答 1

1

尝试改变这个

 ng-options="dept.Name as dept.Name for dept in self.departmentList">
于 2017-02-15T16:31:55.270 回答