Angular UI从指令更新的下拉范围变量不会更新范围

时间:2016-02-03 21:44:49

标签: angularjs angularjs-directive angular-ui-bootstrap

我编写了一个指令,根据按下的键更新范围变量。代码对我来说很好,但变量不会更新,直到焦点重新放在下拉列表或拉下来。下面是html和指令代码:

<button type="button" drop-select ="card.purchaseType">{{card.purchaseType}}</button>

 appModule.directive('dropSelect', function() {
   return {
     scope: {
       purchaseType:'=dropSelect'
     },
     link: function(scope, element, attrs){
       var items =["Retail Purchases","Prescription Purchases", "Retail and Prescription Purchases"];
        element.on('keyup', function(e){
          // Get the character pressed
          var stringChar = String.fromCharCode(e.keyCode), currentItem = scope.purchaseType;
          for(var itemCount = 0; itemCount < items.length; itemCount++){
            if(stringChar == items[itemCount].charAt(0) &&
              currentItem != items[itemCount]){
              scope.purchaseType = items[itemCount];
            }
          }
        });
     }
   }
 })

1 个答案:

答案 0 :(得分:-1)

尝试使用element.on(&#39; keypress&#39;)而不是keyup