angular my97时间控件 指令 封装使用

1.背景

my97 时间控件是非常强大咱们就不说了、在angular页面直接用my97是无法绑定ng-model的。那么咱们先简单把my97的用指令封装下。

2.先看下效果

这里写图片描述

3.封装指令

/**
 * 时间控件 my97 时间 by wwupower
 * my97Datepicker日期控在你选取日期后只是赋予了dom的value。
 * 所有我们应该根据my97Datepicker oncleared 动态给ng-model赋值;
 */
app.directive('powerDatePicker', function () {
    return {
        restrict: 'A',
        require: 'ngModel',
        scope:{
            ngModel:"=",
            dateConfig:"@",//支持my97的参数属性
            onpickedCall:"&",//选择之后回调
            onclearedCall:"&"//清除之后回调
        },
        controller:['$scope', '$http' ,'$timeout','$window','$location','$filter',function($scope, $http,$timeout,$window,$location,$filter){
            //时间格式化过滤
            $scope.$watch("ngModel",function (newValue, oldValue) {             
                $scope.ngModel = $filter('date')(newValue,$scope.dateFmt);
            });
        }],
        link: function (scope, element, attr, ngModel) {
            scope.dateFmt = (attr.datefmt || 'yyyy-MM-dd HH');
            element.val(ngModel.$viewValue);
            function onpicking(dp) {
                var date = dp.cal.getNewDateStr();
                ngModel.$setViewValue(date);
                if(typeof scope.onpickedCall=='function'){
                    scope.onpickedCall();
                }
            }
            function oncleared(){
                ngModel.$setViewValue("");
                if(typeof scope.oncleared=='function'){
                    scope.oncleared();
                }
            }
            element.bind('click', function () {
                init97Date();
            });

            function init97Date(){
                try{
                    //my97 基本数据配置
                    var wdateConfig = {
                            onpicking: onpicking,
                            oncleared: oncleared,
                            dateFmt: scope.dateFmt
                    };
                    var config = "{"+scope.dateConfig+"}";
                    var configObj = (new Function("return "+config))();
                    if(configObj['onpicking']){
                        delete configObj['onpicking'];
                    }
                    if(configObj['onpicking']){
                        delete configObj['oncleared'];
                    }
                    //扩展my97属性
                    wdateConfig = $.extend(wdateConfig,configObj);
                    WdatePicker(wdateConfig)
                }catch (e) {
                    if(console){
                        console.log(e);
                    }
                }
            }
        }
    };
});

4.指令使用

<input ng-model="queryObj.beginTime" id="dcsj" type="text" class="power-from-control power-input-date" power-date-picker />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值