/ *我想要的是
     将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/>


/目的:我想使用角度自定义指令创建日期选择器(max和min),min是日期,而max date是vm结束日期(从服务器接收)/

最佳答案

这是工作示例,

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 />

关于javascript - 将绑定(bind)(ng-model)数据发送到Angularjs自定义指令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28720546/

10-13 02:50