AngularJS:在模态中验证 - 最小日期

时间:2015-01-30 22:22:48

标签: javascript angularjs

我正在尝试验证日期输入,而我的最小日期检查无效。非常感谢协助。

<script type="text/ng-template" id="instructor-form.html">
    <form name="instructorForm" novalidate ng-submit="ok('save')">
        <div class="modal-header">
            <h3 ng-hide="!item.InstructorId">Edit Instructor</h3>
            <h3 ng-hide="item.InstructorId">New Instructor</h3>
        </div>
        <div class="modal-body">
            <div class="row-fluid">
                <div class="span5">
                    <div class="control-group" ng-class="{error: instructorForm.lic.$invalid && instructorForm.lic.$dirty}">
                        <label for="lic">License Expiration Date *</label>
                        <div class="input-prepend input-block-level">
                            <span class="add-on"><i class="icon-calendar"></i></span>
                            <input name="lic" type="date" ng-model="item.LicenseExp" required min="1900-01-01">
                        </div>
                        <span ng-show="instructorForm.lic.$dirty">
                            <span ng-show="instructorForm.lic.$error.required" class="help-inline">License Exp. is required</span>
                        </span>
                        <span ng-show="instructorForm.lic.$error.min">Not a valid date! Please enter a date after 1900.</span>
                    </div>
                </div>
            </div>
        </div>
        <div class="modal-footer">
            <span ng-show="instructorForm.$invalid">* Please enter all required information before saving.</span>
            <button class="btn btn-primary" ng-disabled="instructorForm.$invalid" type="submit">Save</button>
            <button class="btn btn-warning" ng-click="cancel()">Cancel</button>
        </div>
    </form>
</script>

instructorForm.lic。$ error.min是我遇到问题的地方。 。$ dirty和。$ error.required工作得很好,但是。$ error.min似乎没有蹲下。

控制器代码:

$scope.editInstructor = function (orig) {
    dialogScope.item = angular.copy(orig);
    dialogScope.item.LicenseExp = moment(orig.LicenseExp).format('YYYY-MM-DD');

    var modal = $modal.open({
        templateUrl: 'instructor-form.html',
        controller: 'ModalCtrl',
        resolve: { childScope: function () { return dialogScope; } }
    });

    modal.result.then(function (result) {
        if (result === 'save') {
            var absMinDate = new moment('1900-01-01');

            if (moment(absMinDate).isBefore(dialogScope.item.LicenseExp)) {
                // set in our collection for immediate user feedback
                $scope.instructors[idxof(orig)] = dialogScope.item;
                pubSub.publish('highlight', orig.InstructorId);

                // call service to save on server
                dialogScope.item.$save(function(updated) {
                    if (updated.error) {
                        // if failure, reset original in collection
                        $scope.instructors[idxof(orig)] = orig;
                        pubSub.publish('highlight', orig.InstructorId);

                        // show error
                        notificationService.error('<h4>An error occurred updating the instructor.</h4>' + updated.error);
                    } else {
                        // if success, show message
                        notificationService.success('Instructor updated successfully.');
                        cacheManager.removeResource('instructor');
                    }
                });
            } else {
                // show error
                // notificationService.error('<h4>Please enter a date after January 1st, 1900.</h4>');
                // This doesn't work, as it shuts the modal and then displays an error - useless to the user.
            }
        }
    });
};

0 个答案:

没有答案