javascript - Bind function on key up in Angular - Stack Overflow

I am a designerfront-end developer and have little to no experience with Angular, so I'm hoping t

I am a designer/front-end developer and have little to no experience with Angular, so I'm hoping to get some help here. I have the following html

<div class="dropdown">
<div class="options"></div>
  <div class="add">
    <i id="add-issue-plus" class="icon-plus" data-ng-click="addIssue($event)"></i>
    <input id="add-issue-field" data-ng-model="newIssueName" type="text" placeholder="Create a new issue"/>
  </div>
</div>

I would like to trigger the click event from the <i> element if the user presses enter while in the subsequent input. I wanted to do this the simplest way possible without writing a separate function. Anyone with Angular experience know of the best way to do this? I know I could easily use jQuery and do something like:

$('#add-issue-field').keypress(function(e){
   var key = e.which;
   if (key === 13) {
      $('#add-issue-plus').click();
        return false;
  }
 });

But I was wondering if anyone had tips for a more efficient method.

I am a designer/front-end developer and have little to no experience with Angular, so I'm hoping to get some help here. I have the following html

<div class="dropdown">
<div class="options"></div>
  <div class="add">
    <i id="add-issue-plus" class="icon-plus" data-ng-click="addIssue($event)"></i>
    <input id="add-issue-field" data-ng-model="newIssueName" type="text" placeholder="Create a new issue"/>
  </div>
</div>

I would like to trigger the click event from the <i> element if the user presses enter while in the subsequent input. I wanted to do this the simplest way possible without writing a separate function. Anyone with Angular experience know of the best way to do this? I know I could easily use jQuery and do something like:

$('#add-issue-field').keypress(function(e){
   var key = e.which;
   if (key === 13) {
      $('#add-issue-plus').click();
        return false;
  }
 });

But I was wondering if anyone had tips for a more efficient method.

Share Improve this question edited Dec 7, 2015 at 21:21 Mr Lister 46.6k15 gold badges113 silver badges155 bronze badges asked Dec 2, 2015 at 21:03 JordanBarberJordanBarber 2,1016 gold badges37 silver badges64 bronze badges 1
  • Possible duplicate of stackoverflow./questions/17470790/… – Arg0n Commented Dec 2, 2015 at 21:06
Add a ment  | 

3 Answers 3

Reset to default 2

The best use for this is a directive. Here is an example.

app.directive('checkKey', function() {
    return {
        restrict: 'A',
        link: function(scope, elem, attrs) {
            elem.bind('keyup', function(event) {
                if (event.keyCode === 13) {
                    event.preventDefault();
                    return false;
                }
            });
        }
    }
});

And then add the directive to your input element

<input type="text" checkkey />

I think you are pretty close in your thinking. There's a bit more of an angular-centric way to do this though:

If you look on the #add-issue-plus element you'll see an attribute called data-ng-click, this is how you bind methods to elements in angular. To bind to a keypress you would use data-ng-keypress https://docs.angularjs/api/ng/directive/ngKeypress. Then find the controller where the addIssue method is and make a new method that does something similar to what your jQuery above does. Evaluate the key that was pressed and just directly call the addIssue method from above.

dummy html:

<div class="options">
  <div class="add">
    <i id="add-issue-plus" class="icon-plus" data-ng-click="addIssue($event)"></i>
    <input id="add-issue-field" data-ng-keypress="onKeypress($event)" data-ng-model="newIssueName" type="text" placeholder="Create a new issue"/>
  </div>
</div>

...and then somewhere in the angular controller:

$scope.onKeypress = function(event) {
  var key = e.which;
  if (key === 13) $scope.addIssue(event);
};

I have written on this exact directive in the past. You can even create a directive that accepts a key-code and an event making your directive more useable as well.

angular.module("myApp").directive('dlKeyCode', dlKeyCode);

  function dlKeyCode() {
    return {
      restrict: 'A',
      link: function($scope, $element, $attrs) {
        $element.bind("keypress", function(event) {
          var keyCode = event.which || event.keyCode;

          if (keyCode == $attrs.code) {
            $scope.$apply(function() {
              $scope.$eval($attrs.dlKeyCode, {$event: event});
            });

          }
        });
      }
    };
  }

In your HTML you can then do:

<div class="form">
    <input type="text" code="13" dl-key-code="closeModalWindow();" />
    <input type="text" code="24" dl-key-code="submitInformation();" />
</div>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745244755a4618359.html

相关推荐

  • javascript - Bind function on key up in Angular - Stack Overflow

    I am a designerfront-end developer and have little to no experience with Angular, so I'm hoping t

    5小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信