【问题标题】:mat-datepicker show month and year onlymat-datepicker 仅显示月份和年份
【发布时间】:2018-06-28 10:06:19
【问题描述】:

这是我的代码。我正在使用角材料。如何将日历设置为仅显示月份和年份?

<input matInput [matDatepicker]="picker" readonly>
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker startView="year" [startAt]="startDate"></mat-datepicker>

【问题讨论】:

标签: angular material-design angular-material angular-material2


【解决方案1】:

这个功能似乎是最近通过拉取请求添加的

你可以设置视图

this._activeDate = this._monthView ?
this._activeDate = this._currentView == 'month' ?

PULL-REQUEST

但是,通过将模式设置为 angularjs-material,这是可能的

md-mode="month"

【讨论】:

  • 它发布在5.1.0,最新版本。 ?
【解决方案2】:

萨吉塔兰,

this._activeDate = this._monthView ?
this._activeDate = this._currentView == 'month' ?

如何在 Angular 5 中使用 mat-datepicker 以 json 模式形式使用它

【讨论】:

  • 这应该是对他的回答的评论,不是上面问题的答案。
【解决方案3】:

对于带有月份和年份的 Angular Material 日期选择器 --

app.js

var app = angular.module('plunker', ['ngMaterial']);

app.controller('MainCtrl', function($scope) {

  var monthFormat =  buildLocaleProvider("MMM-YYYY");

    function buildLocaleProvider(formatString) {
        return {
            formatDate: function (date) {
                if (date) return moment(date).format(formatString);
                else return null;
            },
            parseDate: function (dateString) {
                if (dateString) {
                    var m = moment(dateString, formatString, true);
                    return m.isValid() ? m.toDate() : new Date(NaN);
                }
                else return null;
            }
        };
    }


  $scope.dateFields = [ 
                {
                    type: 'date',
                    required: false,
                    binding: 'applicant.expectedGraduation',
                    startView: 'month',
                    label: 'Credit Card Expiry - Year/Month picker',
                    mode: 'month',
                    locale: monthFormat
                }
    ];
});

index.html

<!DOCTYPE html>
<html ng-app="plunker">

<head>
  <meta charset="utf-8" />
  <title>AngularJS material-sidenav Plunker</title>
  <script>
    document.write('<base href="' + document.location + '" />');
  </script>
  <link rel="stylesheet" href="https://static.formhero.io/formhero/js/material/v1.1.1-fh-build/angular-material.min.css">
  <link rel="stylesheet" href="style.css" />
  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.15.1/moment-with-locales.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-animate.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular-aria.min.js"></script>
  <script src="https://static.formhero.io/formhero/js/material/v1.1.1-fh-build/angular-material.min.js"></script>
  <script src="app.js"></script>
</head>

<body layout="row" layout-wrap flex layout-fill layout-align="start start" ng-controller="MainCtrl">

    <md-input-container flex="100" layout="column">
          <div style="font-size: 10px; color: blue;" label ng-bind="::dateFields[0].label"></div>
          <md-datepicker ng-model="dateFields[0].selectedDate"
                     ng-required="dateFields[0].required"
                     md-date-locale="dateFields[0].locale"
                     md-mode="{{dateFields[0].mode}}"
                     md-open-on-focus="true">
        </md-datepicker>
    </md-input-container>
</body>

</html>

为了演示使用下面的链接-- https://plnkr.co/edit/zXhgq3?p=preview

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多