将绑定模型数据发送到Angularjs自定义指令

时间:2015-02-25 13:41:50

标签: javascript angularjs

/ *我想要的是什么      将maxDate设置为vmEndDate * /

   app.directive('myDatepicker', function ($parse) {
        return function (scope, element, attrs, controller) {

            var ngModel = $parse(attrs.ngModel);
            alert(element.val());
            $(function(){
                element.datepicker({
                    showOn:"both",
                   // changeYear:true,
                    //changeMonth:true,
                    dateFormat:'dd-MMMM-yyyy',
                    //maxDate: vmEndDate,
                    minDate: new Date(),
                   // yearRange: '1920:2012',
                    onSelect:function (dateText, inst) {
                        scope.$apply(function(scope){
                            // Change binded variable
                            ngModel.assign(scope, dateText);
                        });
                    }
                });
            });
        }
    });

/ * html代码* /

 <input id="dp" type="text" ng-model="vmEndDate" name="mDate" my-datepicker/>

/ 目标:我想创建一个日期选择器(最大和最小)使用角度自定义指令min是日期日期,最大日期是vm结束日期(从服务器接收) /

1 个答案:

答案 0 :(得分:0)

这是工作示例,

app.directive('myDatepicker', function ($parse) {

    return function (scope, element, attrs, controller) {
        var ngModel = $parse(attrs.ngModel);
        var vmEndDate = scope.$eval(attrs.ngModel);
        $(function () {
            element.datepicker({
                showOn: "both",
                // changeYear:true,
                //changeMonth:true,
                dateFormat: 'd-M-yy',
                maxDate: new Date(vmEndDate),
                minDate: new Date() ,
                // yearRange: '1920:2012',
                onSelect: function (dateText, inst) {
                    scope.$apply(function (scope) {
                        // Change binded variable
                        ngModel.assign(scope, dateText);
                    });
                }
            });
        });
    }
});


    <input id="dp" type="text" ng-model="vmEndDate" ng-init="vmEndDate ='27-Feb-2015'" name="mDate" my-datepicker />