Microsoft Edge and Angularjs: ng-model doesn't work with input number type and arrow keys

徘徊边缘 提交于 2019-12-13 07:37:55

问题


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

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


回答1:


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;

          }
        });
      }
    }
  };
});



回答2:


It should work with this. The problem is since you have not initialized in the controller there is an issue with display. It should work, but seemingly doesnt work in Edge. Everything else remains the same.

  <body ng-controller="myCtrl">
    <h1>{{helloworld}}</h1>
    <input type="number" ng-model="md"/>
    <div ng-if="md">{{md}}</div>
  </body>

Check the code here:

https://plnkr.co/edit/9Evcuj6AMP5DXqKZejg7



来源:https://stackoverflow.com/questions/40427514/microsoft-edge-and-angularjs-ng-model-doesnt-work-with-input-number-type-and-a

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!