Angular model does not bind properly to Bootstrap 3 DateTimePicker after 'dp.change' event

I am currently implementing the Bootstrap 3 DateTimePicker plugin by eonasdan. While everything seems to be functioning correctly, I have encountered an issue with binding the selected date within the input field to Angular's ng-model. Whenever I make a date selection using the picker, the 'dp.change' event triggers and updates the date in the input field; however, I need to manually press a key (such as spacebar) for the ng-model to detect the update.

Below is a snippet of the code:

JavaScript with Angular directive:

(function () {
    'use strict';

    var module = angular.module('feedApp');

    module.directive('datetimepicker', [
        '$timeout',
        function ($timeout) {
            return {
                require: '?ngModel',
                restrict: 'EA',
                scope: {
                    options: '@',
                    onChange: '&',
                    onClick: '&'
                },
                link: function ($scope, $element, $attrs, controller) {
                    $($element).on('dp.change', function () {
                        //alert("change");
                        $timeout(function () {
                            var dtp = $element.data('DateTimePicker');
                            controller.$setViewValue(dtp.date());
                            $scope.onChange();
                        });
                    });

                    $($element).on('click', function () {
                        $scope.onClick();
                    });

                    controller.$render = function () {
                        if (!!controller) {
                            if (controller.$viewValue === undefined) {
                                controller.$viewValue = null;
                            }
                            else if (!(controller.$viewValue instanceof moment)) {
                                controller.$viewValue = moment(controller.$viewValue);
                            }
                            $($element).data('DateTimePicker').date(controller.$viewValue);
                        }
                    };

                    $($element).datetimepicker($scope.$eval($attrs.options));
                }
            };
        }
    ]);
})();

HTML:

<div style="max-width: 250px">
    <div class="input-group input-group-sm date">
        <input type="text" class="form-control" options="{format:'DD.MM.YYYY HH:mm', locale:'pl'}" datetimepicker ng-model="feed.reminderDateTime" />
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
    </div>
</div>

Is there a solution to this issue? Any help or suggestions would be greatly appreciated.

Answer №1

I encountered a similar issue in the past. Unfortunately, I don't have the exact source code to refer back to, but I recall that it was something along these lines.

With your ng-model being ng-model="feed.reminderDateTime"

In your controller, make sure to include this line:

$scope.feed = {reminderDateTime:''};

After doing this, you should be able to access it like so: $scope.feed.reminderDateTime

Please let me know how it works out for you.

Answer №2

import ('timepicker');

Vue.directive('timepicker', {
    twoWay: true, //crucial for binding the model data
    bind: function () {
        $(this.el).timepicker({
            format: 'HH:mm:ss',    //refer to moment.js for other valid time formats
            useCurrent: false
        }).on('tp.change', function (value) {
            //action: implement necessary changes to the value here 
        });
    },
    update: function (value) {
        $(this.el).val(value).trigger('tp.change');
    },
    unbind: function () {
        $(this.el).off().timepicker('destroy');
    }
});

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the method for prompting a save as dialog in the browser in order to save JSON data stored in memory?

As the title suggests, I am looking for a way to store JSON data in memory. I want this action to be triggered by an onclick event on a DOM object. The goal is to save the data on the user's computer as if they were downloading a file. Saving it as t ...

Leverage the variable from one function in a different function within Three.js using Javascript

After loading an obj file using three.js, I attempted to obtain the 'X' position of its vertices and save it in a variable named 'pos' inside the objloader function within the init() function. My goal was to access this variable's ...

Retrieve a file from an Express API using React with the help of Axios

When working with a React client and an Express API, how can the React client download a file sent by the Express API? Issue: If I manually enter the URL into my browser's address bar and hit enter, the file downloads successfully. However, when I ...

The putImageData function claims that the given object is not an ImageData object, but upon inspection in the console, it clearly displays that it is

After using getImageData to store the pixels of an image in a two-dimensional array, I attempted to draw them with putImageData. However, I encountered an error indicating that the first parameter is not of type ImageData. Strangely, when I logged the vari ...

What is the process for declaring an exported type variable in React?

I have created a unique configuration in a different file: //config.js export const config = [ { id:0, name:"Config 1", }, { id:1, name:"Config 2", }, { id:2, name ...

Pictures are not showing up in the gallery after they have been saved

if (action.equals("saveToGallery")) { JSONObject obj = args.getJSONObject(0); String imageSource = obj.has("imageSrc") ? obj.getString("imageSrc") : null; String imageName = obj.has(" ...

Comprehending the intricacies of routing within AngularJS

Question: I've been looking into this issue, but there seems to be conflicting answers. I created a simple example in Plunker to understand how routers work in AngularJS, but I'm having trouble getting it to function properly... Below is my inde ...

Challenge with the submission event listener

I am struggling to create my first event listener for the submit button that should respond to both a click and an enter key press. Currently, it is not working for either event, and I am unsure why. I do not intend to send any data to another page; I simp ...

Implementing $timeout within the Scope.$watch function allows for monitoring

Hi there, I'm currently working on implementing some functionality in Angular but running into a few issues. I have an ng-model and example-directive configured as follows: <input ng-model="model" type="text" class="form-control"> <div ex ...

The performance of Jquery Animate is acting strangely after the upgrade to version 1.11

Take a look at http://jsfiddle.net/integerz/k19x8L1b/2/ which uses version 1.7.x $(function () { $("#clickme").toggle(function () { $(this).parent().animate({right:'0px'}); }, function () { $(this).parent().animate({righ ...

Unexpected lag causing delays in jQuery animations

I am attempting to implement a "hover" effect using jQuery. Everything seems to be working fine, except for a strange delay that occurs only the first time the complete callback is triggered - oddly enough, this is the only instance where it reaches the pr ...

Issue with the successful execution of connection event handler in NodeJS and Socket.io?

When I look at my code in files named server.js and index.html, I notice that the io.on('connection') part is not executing the console.log method in its callback when I visit my server in the web browser. Take a look at the code snippets below ...

The most sophisticated method for creating a 2x2 grid of divs

I have developed a quiz application similar to Buzzfeed with four answer options for each question. The layout on mobile devices is satisfactory, displayed in a 2x2 grid. However, when viewing the app on desktop screens, the answers appear horizontally in ...

When utilizing React, I generated an HTML page with a distinct .js file, however, encountered two unexpected errors

Update : Gratitude to everyone who has helped me in tackling this issue. One user suggested using a web server, but my intention was to find a solution using just a single HTML and JS file. Even though I tried following the steps from a similar question o ...

The Angular directive is failing to refresh the data on the Google Map

I created a directive called "myMap" to incorporate a Google map into my application. However, I am facing an issue when attempting to update the longitude and latitude values for a different location using a controller function. The directive does not ref ...

Deploying AngularJS and Ruby on Rails applications on Heroku platform

I have successfully completed an application using Ruby on Rails and AngularJS. It is fully functional and operational on my local machine. However, I am facing difficulties when trying to deploy it to Heroku as I keep encountering the following error: re ...

How can I retrieve the handle for an item within a jQuery collection from within the success function of an .ajax() call?

I'm currently facing an issue with a jQuery ajax call that I have firing for each element in a collection identified by a specific jQuery selector. Here's a snippet of the code: $('.myClass').each(function () { $.ajax({ url ...

"Implementing conditional evaluation for pixel units in AngularJS using ng-style

Looking to adjust the style of an element based on its height. For example, if the height is less than X, apply 'style 1'; otherwise, apply 'style 2'. The syntax appears to be correct, but accurately evaluating the height property in p ...

Leveraging JSON Data for Avatars in React.js

Struggling to arrange 10 Avatars side by side for showcasing the user list. However, encountering an issue where the Avatars are being duplicated and showing incorrect initials when passing JSON data. Experimented with this in Code Sandbox linked below. h ...

Static response is the way to go! Asynchronous responses just don't cut it

Currently in the process of developing an angular directive for displaying real-time charts. Below is the code snippet that encompasses everything, including link: function() { }, within the directive. Here's the code for a static directive that func ...