Ryu Roble Ryu Roble - 2 months ago 80
AngularJS Question

md-datepicker view should display the months only

I am working on a project and I have a md-datepicker on it and I want this md-datepicker to show only the months of the year(January, February, and so on ......). How can I do this?

Here is my code so far:

HTML

<md-datepicker ng-model="currentDate" md-current-view="month" md-date-filter="monthlyDate"></md-datepicker>


JS

var app = angular.module("DashboardApp", ['ngMaterial', 'ngAnimate','md.data.table']);
app.controller('rptController', ['$scope', function ($scope) {
$scope.currentDate = new Date();
$scope.monthlyDate = new Date($scope.currentDate.getMonth('MM'))}]);

Answer

It sounds like you want an md-select rather than a md-datepicker. See example below.

(function() {
  angular
    .module('exampleApp', ['ngAnimate', 'ngAria', 'ngMaterial'])
    .controller('ExampleController', ExampleController);

  function ExampleController() {
    var vm = this;
    vm.monthNames = ["January", "February", "March", "April", "May", "June",
      "July", "August", "September", "October", "November", "December"
    ];
  }
  ExampleController.$inject = [];
})();
<!DOCTYPE html>
<html ng-app='exampleApp'>

<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular-animate.min.js"></script>
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.5.5/angular-aria.min.js"></script>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.min.css">
  <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/angular-material/1.0.9/angular-material.min.js"></script>
</head>

<body ng-controller="ExampleController as vm">
  <md-input-container>
    <md-select ng-model="vm.chosenMonth" placeholder="Select a month">
      <md-option ng-value="month" ng-repeat="month in vm.monthNames">{{ month }}</md-option>
    </md-select>
  </md-input-container>
</body>

</html>