SORU
23 ŞUBAT 2013, CUMARTESİ


AngularJS genel klavye kısayolları oluşturmak için yolu nedir?

Direktif kullanacağım sanırım, ama garip vücut için Direktif ekleyin, ancak belge üzerinde olayları dinlemek gibi görünüyor.

Bunu yapmak için uygun bir yolu nedir?

GÜNCELLEME: AngularJS UI ve basışı Direktifi their gerçekleşme gördüm.

CEVAP
16 EKİM 2013, ÇARŞAMBA


Daha uygun bir şekilde söyleyebilirim (veya "Açısal yol") bir Direktifi eklemek olacaktır. İşte size basit bir (sadece <body> keypress-events öznitelik ekleyin):

angular.module('myDirectives', []).directive('keypressEvents', [
  '$document',
  '$rootScope',
  function($document, $rootScope) {
    return {
      restrict: 'A',
      link: function() {
        $document.bind('keypress', function(e) {
          console.log('Got keypress:', e.which);
          $rootScope.$broadcast('keypress', e);
          $rootScope.$broadcast('keypress:'   e.which, e);
        });
      }
    };
  }
]);

Sizin göreviniz o sadece böyle bir şey yapabilirsiniz:

module.directive('myDirective', [
  function() {
    return {
      restrict: 'E',
      link: function(scope, el, attrs) {
        scope.keyPressed = 'no press :(';
        // For listening to a keypress event with a specific code
        scope.$on('keypress:13', function(onEvent, keypressEvent) {
          scope.keyPressed = 'Enter';
        });
        // For listening to all keypress events
        scope.$on('keypress', function(onEvent, keypressEvent) {
          if (keypress.which === 120) {
            scope.keyPressed = 'x';
          }
          else {
            scope.keyPressed = 'Keycode: '   keypressEvent.which;
          }
        });
      },
      template: '<h1>{{keyPressed}}</h1>'
    };
  }
]);

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Canal TekZoom

    Canal TekZoo

    1 NİSAN 2012
  • Dave Wallace

    Dave Wallace

    27 Kasım 2007
  • LounaTutorials

    LounaTutoria

    10 EYLÜL 2009