I need to pass this code to angularjs. Someone knows how to help me.

-1

Does anyone know how to pass this to angular?

$(document).on(
    'keydown',
    function(e) {
        if (e.ctrlKey && e.altKey
            && e.which === 83) {
        $(".btnSalvarRegistro").click();
    }
);

Please help me.

    
asked by anonymous 09.03.2018 / 17:31

1 answer

0

It is recommended by AngularJS documentation to do event control via policies, but I'll post two examples so that you can see which one will suit your case:

Via Policy

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

app.directive('shortcut', function($document) {
  return {
    restrict: 'E',
    replace: true,
    scope: true,
    link:    function postLink(scope, iElement, iAttrs){
      $document.on('keypress', function(e){
      console.log(e.ctrlKey)
      console.log(e.altKey)
      console.log(e.which)
      console.log("--------")
          if(e.ctrlKey && e.altKey && e.which === 83){
            scope.$apply(scope.changeMessage("letra S +  apertada"));
            e.preventDefault()
          }
      });
    }
  };
});

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

$scope.changeMessage = function(m){
  $scope.message = m
}

});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="app" ng-controller="MainCtrl">
  <shortcut></shortcut>
  <h1>View keys pressed</h1>
  <input type="text">
  {{message}}
</div>

Via Controller

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

app.controller('KeydownController', function KeydownController($scope, $document) {
  $scope.counter = 0
  $document.on("keypress", function (event) {
  
      // AO APERTAR ENTER
      if(event.which === 83) {
          
          $scope.$apply($scope.counter++);
          event.preventDefault();
      }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script><divng-app="app" ng-controller="KeydownController">
  <input type="text">
  {{counter}}
</div>
    
15.03.2018 / 13:19