【问题标题】:Angular Material DatePicker and NG-CHANGEAngular 材质 DatePicker 和 NG-CHANGE
【发布时间】:2016-12-27 20:48:25
【问题描述】:

当用户在 md-datepicker 组件上选择日期时,我需要调用服务器来检查所选日期的日程安排。问题是,我无法使用 ng-change 来触发我的函数,这似乎是这样做的唯一选择。关于为什么这不起作用的任何想法?

form.html

<div
    layout-padding>
    <form
        name="form"
        novalidate
        ng-submit="form.$valid && save()">
        <div
            layout>
        <md-input-container 
            class="md-block" 
            flex="100">
            <label>Nome Completo</label>
            <input 
                name="fullName" 
                ng-model="costumer.fullName" 
                required
                disabled/>
            <div 
              ng-messages="form.fullName.$error">
                <div ng-message="required">O cliente não foi específicado</div>
              </div>
        </md-input-container>
        </div>
        <div
            layout>
        <md-input-container 
            flex>

          <label>Data</label>
          <md-datepicker
              name="date"
              ng-model="appointment.when"
              md-open-on-focus
              md-min-date="today">

          </md-datepicker>

          <div 
              ng-messages="form.date.$error">
            <div ng-message="valid">Data inválida</div>
            <div ng-message="mindate">Data anterior à atual</div>
          </div>
        </md-input-container>

        <md-input-container
            flex="50">
            <label>Horário</label>
            <md-select 
                name="whatTime"
                ng-model="appointment.whatTime"
                required>
                <md-option value="0">08:00 - 09:00</md-option>
                <md-option value="1">09:00 - 10:00</md-option>
                <md-option value="2">10:00 - 11:00</md-option>
                <md-option value="3">13:00 - 14:00</md-option>
                <md-option value="4">14:00 - 15:00</md-option>
                <md-option value="5">15:00 - 16:00</md-option>
                <md-option value="6">16:00 - 17:00</md-option>
                <md-option value="7">17:00 - 18:00</md-option>
            </md-select>

            <div
                ng-messages="form.whatTime.$error">
                <div
                    ng-message="required">Selecione um horário</div>
            </div>
        </md-input-container>
        </div>
        <md-button
            type="submit">Confirmar</md-button>
    </form>
</div>

controller.js

angular.module('Application').controller('NewAppointmentController', 
    ['$scope', '$routeParams', 'CostumerService', 'AppointmentService',
        function($scope, $routeParams, CostumerService, AppointmentService) {
            console.log('NewAppointmentController init');

            console.log('Costumer: ' + $routeParams.costumerId);

            $scope.today = new Date();
            $scope.appointment = {};
            $scope.appointment.when = $scope.today;

            $scope.save = save;
            $scope.checkSchedule = checkSchedule;

            CostumerService.getUniqueById($routeParams.costumerId)
                    .then(function(data){
                        $scope.costumer = data;
                        console.log('Costumer name: ' + data.fullName);
                    }, function(error){
                       console.log('Erro');
                    });

            function save() {
                console.log('Clicked');

                $scope.appointment.costumer = $scope.costumer;

                AppointmentService.save($scope.appointment)
                        .then(function(data) {
                            console.log('Saved');
                        }, function(error){
                            console.log('Error');
                        });
            }

            function checkSchedule() {
                console.log('Changed: ');
            }

    }]);

【问题讨论】:

    标签: angularjs datepicker angular-material


    【解决方案1】:

    这似乎对我有用。请参阅此 CodePen,ngChange with Datepicker。我加了

    <md-datepicker
      name="date"
      ng-model="appointment.when"
      ng-change="checkSchedule()"
      md-open-on-focus
      md-min-date="today">
    </md-datepicker>
    

    每次更改日期时,都会将其记录到控制台。虽然,您似乎确实应该使用$asyncValidators,如Custom Validation 中所述。

    【讨论】:

    • 谢谢你。看起来这是一个缓存问题 ¬¬'' +1 花时间回答
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-17
    • 1970-01-01
    • 2020-07-24
    相关资源
    最近更新 更多