繁体   English   中英

AngularJS填充引导模态中的表单字段

[英]Angularjs fill form fields in bootstrap modal

我正在Angularjs中更改我的Thymeleaf代码,并且我有以下模式:

<div class="modal" id="addLicenseModal" ng-app="myApp">
            <div class="modal-dialog" ng-controller="freeUserController">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"
                            aria-label="Close">
                            <span aria-hidden="true">&times;</span>
                        </button>
                        <h4 class="modal-title">New license</h4>
                    </div>
                    <div class="modal-body">
                    <div ng-show='users.length > 0'> 
                         <!-- <div th:if="${#lists.size(users)}!=0">  -->
                            <form id="addLicenseForm" role="form" action="#"
                                th:action="@{/administration/license}"
                                th:object="${clientLicenseForm}" method="post">
                                <!-- form start -->
                                <div class="box-body">
                                    <div class="form-group" id=existingUser>
                                         <label>Username</label> <select class="form-control select2"
                                            style="width: 100%;" name="user" ng-model="selectedItem" ng-options="user.username for user in users">                                  
                                        </select>
                                    </div>

                                    <!-- <div class="form-group" id=existingUser>
                                        <label>Username</label> <select class="form-control select2"
                                            style="width: 100%;" th:field="*{user}">
                                            <option th:each="user: ${users}" th:value="${user.username}"
                                                th:text="${user.username}"></option>
                                        </select>
                                    </div> -->
                                    <div class="form-group">
                                        <label>Date and time range</label>
                                        <div class="input-group">
                                            <div class="input-group-addon">
                                                <i class="fa fa-clock-o"></i>
                                            </div>
                                            <input type="text" class="form-control pull-right active"
                                                id="reservationtime"> <input type="hidden"
                                                name="startDate" id="selectedStartDate"> <input
                                                type="hidden" name="endDate" id="selectedEndDate">

                                        </div>
                                        <!-- /.input group -->
                                    </div>
                                    <div class="form-group">
                                        <label>Execution number</label><span id="errmsg"
                                            style="float: right; color: red"></span> <input
                                            th:field="*{counter}" id="executionNumber" type="number"
                                            class="form-control" placeholder="executionNumber">
                                    </div>
                                    <div class="form-group">
                                        <label>MAC address</label> <input id="macAddress" type="text"
                                            class="form-control" th:field="*{macAddress}" maxlength="25"
                                            placeholder="MAC address">
                                    </div>
                                    <div class="form-group">
                                        <label>CPU ID</label> <input id="cpuId" type="text"
                                            class="form-control" th:field="*{cpuId}" maxlength="25"
                                            placeholder="CPU ID">
                                    </div>
                                </div>
                            </form>
                        </div> 
                         <p ng-show="users.length == 0">All clients have the own
                            license, you can update a license with UPDATE button.</p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default pull-left"
                            data-dismiss="modal">Close</button>
                        <button  ng-show='users.length > 0' id="createLicenseButton"
                            type="button" class="btn btn-primary">Create license</button>
                        <button ng-show='users.length == 0' id="createLicenseButton"
                            type="button" class="btn btn-primary" disabled>Create
                            license</button>

                    </div>
                </div>
                <!-- /.modal-content -->
            </div>
            <!-- /.modal-dialog -->
        </div>

目前,只有select字段位于Angular中,我无法在name =“ user”中存储正确的信息,例如,它存储了object:7而不是用户名。 从Spring控制器我有

@Override
    @RequestMapping(value = { "/license" }, method = RequestMethod.GET)
    public String license(Model model){
        try{
            model.addAttribute("licenses", administrationService.getClientLicense());
            model.addAttribute("clientLicenseForm", new ClientLicenseForm());
            model.addAttribute("error", false);
        }catch(Exception e){
            LOG.error("Threw exception in AdministrationControllerImpl::license : " + ErrorExceptionBuilder.buildErrorResponse(e));
            model.addAttribute("error",true);
        }
        return "license";
    }

而且我的javascript代码具有以下针对选择的说明:

var app = angular.module('myApp',[]);
app.controller('freeUserController', function($scope, $http) {
    $http({
        method: 'GET',
        url: 'users'
    }).then(function successCallback(response) {
        $scope.users = response.data.result;
        $scope.selectedItem = $scope.users[0].username;
        // this callback will be called asynchronously
        // when the response is available
    }, function errorCallback(response) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
    });

});

选择字段中的错误在哪里? 有可能在Angularjs中全部制作吗? 谢谢

更新:我固定与select as但角度将值设置为字符串:卢卡,不仅卢卡

我已经解决了阅读官方文档并使用以下代码的问题:

ng-options="user.username as user.username for user in users track by user.username"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM