need to pass this Cod to Angularjs someone knows how to help me

Asked

Viewed 34 times

-1

Does anyone know how to turn it to angular?

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

please help me.

1 answer

0

It is recommended by the Angularjs documentation to control events via directives, but I will post two examples so that you see which one will suit your case:

Via Directive

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>

<div ng-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>
<div ng-app="app" ng-controller="KeydownController">
  <input type="text">
  {{counter}}
</div>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.