【问题标题】:Angularjs store date of daterangepicker into form variableAngularjs将daterangepicker的日期存储到表单变量中
【发布时间】:2016-02-12 08:59:38
【问题描述】:

我正在将表单验证从 thymeleaf 切换到 angularjs,我对 daterangepicker 有一些问题。对于角度,我读到了datarangepicker for angular,所以我想用它在我的表单字段中存储开始日期和结束日期。 这是我的 HTML 模式代码:

<div class="modal" id="addLicenseModal" data-ng-app="myApp">
    <div class="modal-dialog" id="addLicenseModaController" data-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 data-ng-show='users.length > 0'>
                <form novalidate class="simple-form" name="newLicenseForm">
                        <!-- form start -->
                        <div class="box-body">
                            <div class="form-group" id=existingUser>
                                <label>Username</label> 
                                <ui-select theme="bootstrap" style="width: 100%;"
                                data-ng-model="newLicense.username" required> <ui-select-match
                                placeholder="Select username">
                            {{$select.selected.username}}</ui-select-match> <ui-select-choices
                                repeat="user.username as user in (users | filter: $select.search) track by user.username">
                            <span data-ng-bind="user.username"></span> </ui-select-choices> </ui-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 date-range-picker id="daterange1" name="daterange1" class="form-control date-picker" type="text"
                                        data-ng-model="newLicense.date" required/>
                            </div>
                            <div class="form-group">
                                <label>Execution number</label><span id="errmsg"
                                    style="float: right; color: red"></span> <input
                                    data-ng-model="newLicense.counter" id="executionNumber" type="number"
                                    class="form-control" placeholder="executionNumber" min="0" required>
                            </div>
                            <div class="form-group">
                                <label>MAC address</label> <input id="macAddress" type="text"
                                    class="form-control" data-ng-model="newLicense.macAddress" maxlength="25"
                                    placeholder="MAC address" required>
                            </div>
                            <div class="form-group">
                                <label>CPU ID</label> <input id="cpuId" type="text"
                                    class="form-control" data-ng-model="newLicense.cpuId" maxlength="25"
                                    placeholder="CPU ID" required>
                            </div>
                        </div>
                    </form>
                </div>
                <p data-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 data-ng-show='users.length > 0' data-ng-disabled="newLicenseForm.$invalid"
                    data-ng-click="createLicense(newLicense)" id="createLicenseButton"
                    type="button" class="btn btn-primary">Create license</button>
                <button data-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>

在javascript中我有这个代码

var app = angular.module('myApp',['daterangepicker','ui.select']);
app.controller('freeUserController',['$scope','$http', function($scope, $http) {
    $scope.datePicker.date = {startDate: null, endDate: null};
    //Create new user from modal
    $scope.createLicense = function(newLicense) {
        $http({
            method: 'POST',
            url: '',
            headers: {'Content-Type': 'application/json'},
            data : newLicense,
        }).then(function successCallback(response) {
            if (response.data.success==true){   
                ajaxResultPost();
                licenseTable.ajax.reload();
                $('#addLicenseModal').modal("hide");
                notifyMessage(data.result, 'success');
            } else {
                notifyMessage(response.data.result, 'error');
            }                       
        }, function errorCallback(response) {
            window.location.href = "/ATS/500";
        });

    };
}]);

我在$scope.datePicker.date = {startDate: null, endDate: null}; 上收到异常Cannot set property 'date' of undefined 排。有人用过这个插件来存储表单信息吗?谢谢

【问题讨论】:

    标签: javascript angularjs forms datetimepicker bootstrap-datetimepicker


    【解决方案1】:

    您的问题在于数据绑定,特别是这里:

     <input date-range-picker id="daterange1" name="daterange1" class="form-control date-picker" type="text"  data-ng-model="newLicense.date" required/> 
    

    因为从未定义过$scope.newLicense.date

    为了解决这个问题,这行代码:

     $scope.datePicker.date = {startDate: null, endDate: null};
    

    应该是这样的:

     $scope.newLicense = {};
     $scope.newLicense.date = {startDate: null, endDate: null};
    

    在您的 ajax 帖子中,您的数据也应该是 $scope.newLicense

    所以你的控制器应该是这样的:

     var app = angular.module('myApp',['daterangepicker','ui.select']);
        app.controller('freeUserController',['$scope','$http', function($scope, $http) {
            $scope.newLicense = {};
            $scope.newLicense.date = {startDate: null, endDate: null};
            //Create new user from modal
            $scope.createLicense = function(newLicense) {
                $http({
                    method: 'POST',
                    url: '',
                    headers: {'Content-Type': 'application/json'},
                    data : $scope.newLicense,
                }).then(function successCallback(response) {
                    if (response.data.success==true){   
                        ajaxResultPost();
                        licenseTable.ajax.reload();
                        $('#addLicenseModal').modal("hide");
                        notifyMessage(data.result, 'success');
                    } else {
                        notifyMessage(response.data.result, 'error');
                    }                       
                }, function errorCallback(response) {
                    window.location.href = "/ATS/500";
                });
    
            };
        }]);
    

    这是working demo

    【讨论】:

    • 它给了我同样的错误 TypeError: Cannot set property 'date' of undefined at new (license.js:97)
    • 好的,我正在做一个演示
    • 有了这个演示,但我的代码没有plnkr.co/edit/SONepeBWfKhNYFXOLalq?p=preview。现在错误出现在数据范围插件 TypeError: Cannot read property 'startDate' of undefined at Array. (angular-daterangepicker.js:93)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 1970-01-01
    相关资源
    最近更新 更多