2

我正在尝试在 angularjs 中自动聚焦日期字段。我已经为它编写了一个指令,它工作正常,但我遇到了一个问题。HTML5 日期选择器不会打开,除非我不点击右侧出现的图标。

(function () {
  "use strict";

  angular
    .module("ctraUIComponents")
    .directive("inputAutoFocus", autoFocus);

  /**
   * @ngInject
   * @return {{restrict: string, link: autoFocusLink}}
   */
  function autoFocus ($timeout) {
    /**
     * @type {{restrict: string, link: autoFocusLink}}
     */
    var directive = {
      restrict: "A",
      link: autoFocusLink
    };

    return directive;

    /**
     * @ngInject
     * @param scope
     * @param element
     */
    function autoFocusLink (scope, element) {
      $timeout(function() {
        element[0].focus();
      });
    }
  }
})();

有人可以帮我弄清楚如何在自动对焦上打开日期选择器,以及当我们单击输入字段上的任意位置时如何打开日期选择器。

谢谢

4

1 回答 1

0

`

<div layout-gt-xs="row">
    <div flex-gt-xs>
      <h4>Opening the calendar when the input is focused</h4>
      <md-datepicker ng-model="ctrl.myDate" md-placeholder="Enter date" md-open-on-focus></md-datepicker>
    </div>
</div>

`

angular.module('MyApp', ['ngMaterial', 'ngMessages', 'material.svgAssetsCache']).controller('AppCtrl', function() { this.myDate = new Date(); this.isOpen = false; });

有关更多示例,请遵循此Angular JS Material 示例链接

于 2020-11-09T07:59:33.917 回答