"Exploring the world of AngularJS Datepicker alongside the power

My desire is to have a calendar that displays only the option of selecting a month and year, with the format being displayed as "yyyy-mm". Once the month and year are selected, I need it to update the ng-model variable value in the specified format. I've attempted using several jquery ui-datepicker options within my angularjs file without success. Any assistance would be greatly appreciated.

Answer №1

This custom directive using a bootstrap date picker can be very useful.

If you are interested in the Bootstrap Date Picker, you can find more information at this URL: https://github.com/Eonasdan/bootstrap-datetimepicker

Current Version: 4.17.37

directives.directive("customDatepicker", function () {
        return {
            restrict: 'A',
            require: "ngModel",
            link: function (scope, element, attrs, ngModelCtrl) {
                element.datetimepicker({
                    format: 'MM/YYYY',
                    useCurrent: false,
                    useStrict: true,
                    maxDate: 'now',
                    ignoreReadonly: false,  
                })
                  .on("dp.change", function (e) {
                      scope.dateValid = moment(e.date).format("MM/YYYY");
                      ngModelCtrl.$setViewValue(moment(e.date).format("MM/YYYY"));
                      scope.$apply();
                  })
                  .on("blur", function (e) { 
                    if(ngModelCtrl.$modelValue && ngModelCtrl.$modelValue.length === 7 && moment(ngModelCtrl.$modelValue,'MM/YYYY').isValid()){
                          scope.dateValid = ngModelCtrl.$modelValue;
                           ngModelCtrl.$setViewValue(ngModelCtrl.$modelValue);
                           scope.$apply();
                          } else {
                            if(scope.dateValid) {
                                ngModelCtrl.$setViewValue(scope.dateValid);
                            } else {
                            ngModelCtrl.$setViewValue();    
                            }
                           scope.$apply(); 
                        }
                  });  
            },
        };
    });   

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

Having difficulty generating dynamic rows and tree dropdowns in AngularJS

Struggling to implement dynamic row functionality with Angular JS. The rows are working well, but I also need to incorporate a tree dropdown within each row. Unfortunately, clicking the "add row" button populates the same data in all rows. I have shared m ...

iOS 10's autofocus feature experiencing difficulties in focusing on input

While using an application on my desktop or Android device, I have noticed that the input focus works perfectly fine. However, when I try to run the same application on iOS 10 Safari, the input focus does not seem to be working. It is worth noting that I ...

The operation of "grunt build" results in a Lexer Error, causing the ng-include

After deploying my unminified code successfully, I proceed to run grunt build and deploy from the dist folder. However, upon checking one of the pages, I encounter a breakage with an error in the console: Error: [$parse:lexerr] Lexer Error: Unexpected nex ...

AngularJS can be used to display a webpage

After facing the need to print a given page using AngularJS, I came up with a simple solution as shown below: <div class="modal fade" id="extrait" aria-hidden="true" data-backdrop="false"> <table class="table table-hover table-bordered" i ...

Relocating the Asp.Net WebAPI to its own domain apart from the Asp.Net MVC site in preparation for the AngularJS

We currently have an Asp.Net MVC 5.0 project that utilizes AngularJS for the front end. The project includes a WebAPI Controller within the same project and is deployed on the same website. This application was created using the Individual User Account pro ...

Adjust the color of the button and update the text using AngularJS

i have been attempting repeatedly, but to no avail. Here is my button: <div ng-class="User.active? 'btn btn-danger' : 'btn btn-success' " ng-click="User.active=!User.active"> {{ User.active ? 'Desactive' : &apo ...

To set up Laravel 5 project on Mac OS X, run `npm install` and hang

My Development Environment ⚡️ Node Version: v8.9.4 ⚡️ NPM Version: 5.6.0 ...

Dynamic Binding of ng-model to DOM Element in AngularJS

I am facing a challenge with my web page where I need to dynamically attach ng-model attributes to some HTML elements that I don't have the ability to edit. What I want to achieve is to have AngularJS re-bind these attributes to the scope. You can fin ...

Having trouble getting $compile to work in an injected cshtml file with Angular

Summary I am currently working on a large application where everything is designed to be very generic for easy expansion. One of the components I am focusing on is the dialog. Before suggesting alternatives like using ngInclude or angular templates, let m ...

AngularJS http requests fail to include session cookies when making CORS requests

Here is the script I am using: <script> function eventController($scope, $http) { $http.get("http://example.com/api/Event", {withCredentials: true}) .success(function (response) { $scope.even ...

Ordering and displaying data with AngularJS

Trying to maintain a constant gap of 5 between pagination elements, regardless of the total length. For instance, with $scope.itemsPerPage = 5 and total object length of 20, we should have 4 pages in pagination. However, if $scope.itemsPerPage = 2 and tota ...

Error: Unsupported Media Type when attempting to send JSON data from an AngularJS frontend to a Spring controller

Below is the controller function code snippet @RequestMapping(value = "/logInChecker", method = RequestMethod.POST, consumes = {"application/json"}) public @ResponseBody String logInCheckerFn(@RequestBody UserLogData userLogData){ Integer user ...

Experimenting with a select menu, angularjs, and jasmine testing framework

I am currently working on a select box that filters based on the selection in another select box. I am trying to create an end-to-end test for this functionality. Here is the code snippet: <select id="inputPipe" ng-model="selectedPipe" ng-options="p.t ...

Utilizing an Angular Service within a method, embedded in a class, nested inside a module

module Helper { export class ListController { static handleBatchDelete(data) { // Implementing $http functionality within Angular ... $http.post(data) } } } // Trigger on button click Helper.ListController. ...

issues with jasmine angularjs tests exhibiting erratic outcomes

During the execution of my unit tests, I often encounter a scenario where some random test(s) fail for a specific controller without any apparent reason. The error messages typically look like this: Expected spy exec to have been called with [ Object({}) ...

Maximizing the potential of views and subviews in AngularJS

I'm a newcomer to AngularJS and facing some challenges with implementing multiple views. Can someone provide guidance on how to achieve this? My goal is to create a file explorer with two columns: the left column displays subfolders and files, while ...

create a word with only **one** bold letter in the word ionic-angularjs

Can someone please guide me on how to style only a specific letter within a word using angularJS? Specifically, I want to make the first letter bold while keeping the rest of the word in normal font. For instance, if I have an array of words like: var Wor ...

Is there a way to dynamically update the text of $ionicPopup's subTitle in Ionic?

I am currently attempting to modify both the value and style of the subText attribute linked to an $ionicPopup within my app. Despite searching extensively, I have been unable to uncover a viable method for accomplishing this task. Is there a way to achi ...

Issue with model not being updated after making a request using $http service

I have a hunch as to why it's not functioning properly, but I'm unsure on how to troubleshoot this. Despite looking at similar questions and answers on SO, I am unable to resolve my issue. My goal is to trigger a service upon page load in order ...

How can the execution of a directive within another directive's template be triggered?

Is there a way to declare a directive within another custom directive template? The goal is for the inner directive to utilize a field from the outer directive's scope: angular.module('core.directives') .directive('containertable&apos ...