Microsoft Edge和Angularjs:ng-model不适用于输入数字类型和箭头键 [英] Microsoft Edge and Angularjs: ng-model doesn't work with input number type and arrow keys

查看:134
本文介绍了Microsoft Edge和Angularjs:ng-model不适用于输入数字类型和箭头键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个type="number"输入,它与一个ng-model相关联.我希望能够按下上下箭头来更新其值.但是在这种情况下,在Microsoft Edge中ng-model不会更新,请在此处查看我的示例:

I have a input with type="number", it is associated with a ng-model.I want to be able to press up and down arrows to update its value. However in Microsoft Edge the ng-model is not updated in this case, see my example here:

https://plnkr.co/edit/yZaGxkYG87C1YCNSKnf0?p=preview

是否有一种不使用jquery来更新模型的方法?我们要避免在控制器中使用jquery

Is there a way of updating the model, that's not using jquery? We want to avoid using jquery in controllers

推荐答案

因此,这是angularjs和Edge的一个错误,如以下讨论所示:

So it's a bug from both angularjs and Edge, as seen in the discussion below:

https://github.com/angular/angular.js/issues/15366

提出了一个片段来自行解决问题:

A snippet is proposed to fix the problem by oneself:

.directive('input', () => {
  if(currentBrowser!="edge") return {};

  return {
    link: (scope, elem) => {
      if (elem[0].type === 'number') {
        elem.on('keydown', evt => {
          switch (evt.which) {
            case 38:   // UP_ARROW
            case 40:   // DOWN_ARROW
              scope.$evalAsync(() => elem.triggerHandler('change'));
              break;

          }
        });
      }
    }
  };
});

这篇关于Microsoft Edge和Angularjs:ng-model不适用于输入数字类型和箭头键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆