Giter Site home page Giter Site logo

Comments (9)

kelvinatorr avatar kelvinatorr commented on June 8, 2024

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:

from clockpicker.

 avatar commented on June 8, 2024

Thanks, done.

from clockpicker.

rtrompier avatar rtrompier commented on June 8, 2024

Hi,
Does it work for you ?

Thx

from clockpicker.

rtrompier avatar rtrompier commented on June 8, 2024

Can you send me your directive please ?

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

from clockpicker.

maliarov avatar maliarov commented on June 8, 2024

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>

from clockpicker.

evdoks avatar evdoks commented on June 8, 2024

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;
                });
            }
        }
    }]);

from clockpicker.

Prashanth-iNautix avatar Prashanth-iNautix commented on June 8, 2024

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

from clockpicker.

Domitnator avatar Domitnator commented on June 8, 2024

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

from clockpicker.

strokyl avatar strokyl commented on June 8, 2024

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

from clockpicker.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.