datepicker值不使用自定义指令绑定到角度模型

时间:2013-11-15 13:58:59

标签: javascript angularjs datepicker

我正在使用datepicker并想要绑定它的值我为它写了自定义指令但是知道它为什么没有绑定值。

HtmlTagsdatepicker

<div class="col-md-6">
                            <label for="datepicker" class="col-lg-5 form-label">Date Of Birth:</label>
                            <div class="col-lg-7">
                                <input type="text" class="form-control dateBirth"  ng-model="user.dob" id="datepicker" name="datepicker" datepicker="" value="" placeholder="Date Of Birth" required/>
                                <div class="error" ng-show="newUser_form.datepicker.$dirty && newUser_form.datepicker.$invalid">
                                    <small class="error errorFields" ng-show="newUser_form.datepicker.$error.required">
                                        Date is required.
                                    </small>

                                </div>
                            </div>
                        </div

&GT;

datepickerDirective.js

app.directive('datepicker', function() {
    var linker = function(scope, element, attrs, ngModelCtrl) {
        element.datepicker().on('changeDate', function(value){
            var dateStrToSend = value.date.getFullYear() + '/' + (value.date.getMonth() + 1) +  '/' + value.date.getDate();
            ngModelCtrl.$setViewValue(dateStrToSend);
            $(".datepicker").hide();
        });
    }
    return {
        require: 'ngModel',
        restrict: 'A',
        link: linker
    }
});

1 个答案:

答案 0 :(得分:0)

我认为你不是在消化周期。试试$apply

element.datepicker().on('changeDate', function(value){
    var dateStrToSend = value.date.getFullYear() + '/' + (value.date.getMonth() + 1) +  '/' + value.date.getDate();
    scope.$apply(function () {
        ngModelCtrl.$setViewValue(dateStrToSend);
    });
    $(".datepicker").hide();
});