clockpicker icon indicating copy to clipboard operation
clockpicker copied to clipboard

Is there an angular support to this plugin?

Open vijaivp opened this issue 10 years ago • 9 comments

Is there an angular directive in existence that will load this jquery plugin.

vijaivp avatar Nov 05 '14 13:11 vijaivp

You can use something like this

app.directive("clockPicker", function(){ return { restrict: "A", link: function(scope,element,attrs) { // Initialize the clockpicker with options.
element.clockpicker({ placement: 'top', donetext: 'Done' }); } } });

The to use it:

kelvinatorr avatar Nov 05 '14 16:11 kelvinatorr

Thanks, done.

ghost avatar Nov 14 '14 13:11 ghost

Hi, Does it work for you ?

Thx

rtrompier avatar Dec 19 '14 15:12 rtrompier

Can you send me your directive please ?

Because my model is not updated. View is ok ;)

rtrompier avatar Dec 19 '14 16:12 rtrompier

Best what I have within 5 mins.

(function () {

    function directive (parse) {
        function link (scope, elm, attrs, ngModel) {

            var inputElm = $('input', elm);
            var modelGetter = parse(attrs['ngModel']);
            var modelSetter = modelGetter.assign;

            function afterUpdate () {
                var value = modelGetter(scope);
                if (value) {
                    value = moment(value);
                }

                var inputVal = moment(inputElm.val(), 'HH:mm');
                if (inputVal.isValid()) {
                    if (!value) {
                        modelSetter(scope, inputVal.toDate());
                    } else {
                        value.hour(inputVal.hour());
                        value.minute(inputVal.minute());

                        modelSetter(scope, value.toDate());

                        scope.$digest();
                    }
                }
            }

            elm
                .clockpicker({
                    donetext: 'Done',
                    autoclose: true,
                    afterDone: afterUpdate
                });


            inputElm.blur(afterUpdate);

            ngModel.$formatters.push(function (value) {
                if (value) {
                    inputElm.val(moment(value).format('HH:mm'));
                }

                return value;
            });

        }

        return {
            restrict: 'C',
            require: 'ngModel',
            link: link
        };
    }

    angular
        .module('clockpicker')
        .directive('clockpicker', ['$parse', directive]);
})();
  <div class="input-group clockpicker" ng-model="someScopeVariable">
    <input type="text" class="form-control" />
      <span class="input-group-addon">
        <span class="glyphicon glyphicon-time"></span>
      </span>
   </div>

maliarov avatar Jan 17 '15 23:01 maliarov

Thanks a lot, @mujichOk !

The only thing I had to change to make model updates working is to add $timeout to afterUpdate function. So my directive looks like this:

angular.module('myApp')
    .directive('clockpicker', ['$timeout', '$parse', function ($timeout, $parse) {
        return {
            restrict: 'C',
            require: 'ngModel',
            link: function link(scope, elm, attrs, ngModel) {
                var inputElm = $('input', elm);
                var modelGetter = $parse(attrs['ngModel']);
                var modelSetter = modelGetter.assign;
                function afterUpdate() {
                    return $timeout(function () {
                        var value = modelGetter(scope);
                        if (value) {
                            value = moment(value);
                        }
                        var inputVal = moment(inputElm.val(), 'HH:mm');
                        if (inputVal.isValid()) {
                            if (!value) {
                                modelSetter(scope, inputVal.toDate());
                            } else {
                                value.hour(inputVal.hour());
                                value.minute(inputVal.minute());

                                modelSetter(scope, value.toDate());

                                scope.$digest();
                            }
                        }
                    })
                }
                elm.clockpicker({
                        donetext: 'Done',
                        autoclose: true,
                        afterDone: afterUpdate
                    });
                inputElm.blur(afterUpdate);
                ngModel.$formatters.push(function (value) {
                    if (value) {
                        inputElm.val(moment(value).format('HH:mm'));
                    }
                    return value;
                });
            }
        }
    }]);

evdoks avatar Jun 24 '15 15:06 evdoks

First thanks for the above answers. However, I am unable to update my model. Please advise @mujichOk @evdoks . The old value still persists.

Prashanth-iNautix avatar Aug 14 '15 10:08 Prashanth-iNautix

@Prashanth-iNautix : Try to change scope.$digest(); to scope.$apply(); In my case this did the trick!

Domitnator avatar Jan 07 '16 13:01 Domitnator

https://github.com/linagora/angular-clockpicker, it's available on bower under the name angular-clockpicker

strokyl avatar Jan 26 '16 15:01 strokyl